1

I have a problem showing my progress bar when reading a file in Java. All works as intended, user choose a file, the program must show the progress bar (but it loads an empty blank frame), process the file and then load the results on another window.

I can't get the program to show the content of the progress bar dialog.
A little help here would be really appreciated.

Here is the code of the 3 methods involved.

//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();
}

Solved with SwingWorker, i post a summarized code:

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 contains all the methods to work with GUIs, showProgressBar() is used to show the bar and closeProgressBar() do the opposite. Thank you guys!

  • 1) Did you have a question? 2) Don't block the EDT, but perform GUI updates **on** the EDT. E.G. use a `SwingWorker` 3) For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Feb 07 '12 at 12:26

1 Answers1

2

Short of more useful code, I suggest using a SwingWorker.

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

Given the nature of the task, you might also look at 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.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    I'm too late, right +1 @Federico Matias Rodriguez please read tutorial about JProgressBar http://docs.oracle.com/javase/tutorial/uiswing/components/progress.html, – mKorbel Feb 07 '12 at 12:33