3

Can't figure this one out. Using worker or invokeLater, the UI still freeze. After each file is downloaded, I want a JList to be updated. But the JList will only update after the tread returns.

Here is the code:

public class MyUi extends javax.swing.JFrame{
    ...

   private void jButton2ActionPerformed(java.awt.event.ActionEvent evt){

      SwingUtilities.invokeLater(new Runnable() {
         //To get out of the event tread
         public void run() {
            dl(); 
         }
       });
   }

   private void dl(){
      ...
      //ini and run the download class
      Download myDownload = new Download();
      myDownload.doDownload(myDlList);
   }

   public void updateJlist(String myString){

       myModel.addElement(myString);
       jList1.repaint();
   }

}

public class Download{
...

  public void doDownload(String[] fileName){
      for(int i=0; i<fileName.length; i++){
         ...//download action...
         //for my jList1 to be updated after each file.
         MyUi.updateJlist(fileName[i]);
      }
   }

}

Any example would help.

gnat
  • 6,213
  • 108
  • 53
  • 73
marcb
  • 142
  • 1
  • 2
  • 12

3 Answers3

4

invokeLater does exactly the opposite of what you expect it to do - it runs operations on the EDT, which explains the behaviour.

Hauke Ingmar Schmidt
  • 11,559
  • 1
  • 42
  • 50
3

Download the file on a background thread and wrap just updateJlist() in a Runnable.

SwingWorker would be more reliable.

Addendum: As @mre notes, SwingWorker also makes it easy to report interim results, as shown here.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Running the Download in the background is working just fin and i don't need to wrap updateJlist(). For SwingWorker, i will need to read a bit more on it and perform test. I don't fully understand it. Thanks! – marcb Jan 26 '12 at 01:51
  • I'm glad it's working, but I remain [wary](http://stackoverflow.com/a/7158505/230513) of updating the GUI from a non-EDT thread. Don't hesitate to post a question if you need help with `SwingWorker`. – trashgod Jan 26 '12 at 06:38
  • 1
    @marcb it's simply plain **wrong** to update the list off the EDT, even if it appears to be working right now. You **must** access all of a Swing component's properties (trivially including any models) on the EDT, no exception to that rule. – kleopatra Jan 26 '12 at 10:02
  • I have deleted my comment "Final code:" since i now realise that even if it worked perfectly, it was a big mistake and bad coding. I will keep reading on the SwingWorker, clean my code and i will then post back my issue and concern. Thanks for the support. – marcb Jan 27 '12 at 01:50
0

I have create a WorkerThread class which take care of Threads and GUI current/main thread . i have put my GUI application in construct() method of WorkerThread when an event fire to start XXXServer then all threads are activate and GUI work smoothlly wihout freeze. have a look.

/** * Action Event * * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent) */

public void actionPerformed(ActionEvent ae) { log.info("actionPerformed begin..." + ae.getActionCommand());

try {
    if (ae.getActionCommand().equals(btnStart.getText())) {
         final int portNumber = 9990;
         try {

             WorkerThread workerThread = new WorkerThread(){
                public Object construct(){

                    log.info("Initializing the Server GUI...");
                    // initializing the Server
                     try {
                        xxxServer = new XXXServer(portNumber);
                        xxxServer.start();
                        btnStart.setEnabled(false);                             
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        log.info("actionPerformed() Start button ERROR IOEXCEPTION..." + e.getMessage());
                        e.printStackTrace();
                    }
                    return null;
                }
            };workerThread.start();
            } catch (Exception e) {
                log.info("actionPerformed() Start button ERROR..." + e.getMessage());
                e.printStackTrace();
         }


    } else if (ae.getActionCommand().equals(btnStop.getText())) {
        log.info("Exit..." + btnStop.getText());
        closeWindow();
    }

} catch (Exception e) {
    log
        .info("Error in ServerGUI actionPerformed==="
            + e.getMessage());
}

}

mkumar0304
  • 637
  • 7
  • 8