Java's Swing Threading Java's Swing Threading multithreading multithreading

Java's Swing Threading


In my opinion you should almost never use invokeAndWait(). If something is going to take awhile that will lock your UI.

Use a SwingWorker for this kind of thing. Take a look at Improve Application Performance With SwingWorker in Java SE 6.


You should consider using SwingWorker since it will not block the UI thread, whereas both SwingUtilities methods will execute on the EDT thread, thus blocking the UI.


I keep the simple Thread inside EventQueue.invokeLater(...) and that worked smoothly...

java.awt.EventQueue.invokeLater(new Runnable() {    public void run(){        new Thread(new Runnable(){            public void run(){                try{                    EdgeProgress progress = EdgeProgress.getEdgeProgress();                    System.out.println("now in traceProgressMonitor...");                    while(true){                        // here the swing update                        if(monitor.getState() == ProgressMonitor.STATE_BUSY){                            System.out.println(monitor.getPercentDone()/2);                            progress.setProgress(monitor.getPercentDone()/2);                        }else{                            break;                        }                        Thread.sleep(5);                    }                }catch(InterruptedException ie){}            }        }).start();    }});