java - JPanel 未在 JFrame 中加载

标签 java swing jframe jpanel jprogressbar

在 Java 中读取文件时,我在显示进度条时遇到问题。 一切都按预期进行,用户选择一个文件,程序必须显示进度条(但它加载一个空的空白框架),处理文件,然后将结果加载到另一个窗口上。

我无法让程序显示进度条对话框的内容。
如果您能在这里提供一点帮助,我们将不胜感激。

这是涉及的3个方法的代码。

//this method reads the file
public void processFile(File arch) {       
  aFile = arch;
  Thread threadForSearch = new Thread() {
  @Override
  public void run() {
      try{
         listaProveedoresTango = controladoraConsultas.traerProveedores();  
         listaProveedoresAFIP = new LinkedList();
     BufferedReader data = new BufferedReader(new FileReader(aFile));
     String s;
      while ((s = data.readLine()) != null) {                  
     //long task                 
      }
      data.close();
    }catch (Exception e){
      System.err.println("Error: " + e.getMessage());
    }
      }
    };

    interfacesController.loadProgressBar();

    threadForSearch.start();         

   try {
      threadForSearch.join();
   } catch (InterruptedException ex) {
      Logger.getLogger(Controladora.class.getName()).log(Level.SEVERE, null, ex);
   }
   this.interfacesController.closeProgressBar();
   this.interfacesController.loadResults(someStuff);       
}

//load a progress bar
public void loadProgressBar(){           
  JProgressBar pb = new JProgressBar(0,100);
  pb.setPreferredSize(new Dimension(175,20));
  pb.setString("Processing Data");
  pb.setStringPainted(true);
  pb.setIndeterminate(true);
  JLabel infoLabel = new JLabel("Reading File: ");
  JButton cancelButton = new JButton("Cancel");
  cancelButton.addActionListener(new AbstractAction() {
    @Override
    public void actionPerformed(ActionEvent evt) {
      exitSystem();
    }
  });
  cancelButton.setVerticalAlignment(SwingConstants.CENTER);
  JPanel center_panel = new JPanel();
  center_panel.add(infoLabel);
  center_panel.add(pb);
  center_panel.add(cancelButton);
  center_panel.setLayout(new BoxLayout(center_panel,BoxLayout.Y_AXIS));
  dialog = new JDialog((JFrame)null, "Processing ...");
  dialog.getContentPane().add(center_panel, BorderLayout.CENTER);
  dialog.setSize(100, 100);
  dialog.setLocationRelativeTo(null);
  dialog.pack();
  dialog.setVisible(true);          
}

//close the open progress bar
public void closeProgressBar(){
   this.dialog.dispose();
}

用 SwingWorker 解决了,我发布了总结代码:

public void processFile(File arch) {

    aFile = arch;

    final SwingWorker searchOnFile = new SwingWorker(){  

      @Override  
      protected Object doInBackground() throws Exception {  
        try{
            BufferedReader data = new BufferedReader(new FileReader(aFile));
            String s;
            while ((s = data.readLine()) != null) {                  
                //long task                  
             }
             data.close();
        }catch (Exception e){ //Catch exception if any
            System.err.println("Error: " + e.getMessage());
        }
     interfacesController.closeProgressBar();
     interfacesController.loadResults(someStuff); 
     return null;
     }
   };  

   interfacesController.showProgressBar(); 

   searchOnFile.execute();

}

interfacesController 包含使用 GUI 的所有方法,showProgressBar() 用于显示栏,而 closeProgressBar() 则执行相反的操作。谢谢大家!

最佳答案

缺少更有用的代码,我建议使用 SwingWorker .

An abstract class to perform lengthy GUI-interaction tasks in a background thread. Several background threads can be used to execute such tasks. ..

鉴于任务的性质,您还可以查看 ProgressMonitorInputStream .

..creates a progress monitor to monitor the progress of reading the input stream. If it's taking a while, a ProgressDialog will be popped up to inform the user. If the user hits the Cancel button an InterruptedIOException will be thrown on the next read. All the right cleanup is done when the stream is closed.

关于java - JPanel 未在 JFrame 中加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9176178/

相关文章:

java - 收件人较多时无法发送电子邮件

java - 如何将实际jFrame的实例传递给其他jFrame

java - 使用许多 jFrame

java - 为什么我的 JFrame 不显示?

Macbook Bootcamp Windows 上的 Java Html 呈现器

java - 循环FileReader

java - 应用程序范围的键盘快捷键 - Java Swing

java - 如何使用removeAll()从JFrame中删除所有组件?

Java:如何计算ArrayList中不重复(只出现一次)的字符串?

Gnome下的Java Swing应用——使用Adwaita(深色皮肤)窗口标题栏