3

im calling invokeLater direcly from button on actionPerformed with this code:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
   SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            int temp = (jComboBox1.getSelectedIndex() + 1);
        heavyProccesingFunction();
        }
   });
} 

and that still freezes the GUI. Why? I get the same result without using the invokelater function.

should I Use

Thread queryThread = new Thread() {
      public void run() { 

instead?

Edit:

Thanks, new thread should be used.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Tom
  • 109
  • 2
  • 9
  • "If invokeLater is called from the event dispatching thread -- for example, from a JButton's ActionListener -- the doRun.run() will still be deferred until all pending events have been processed. Note that if the doRun.run() throws an uncaught exception the event dispatching thread will unwind (not the current thread)." http://download.oracle.com/javase/1.4.2/docs/api/javax/swing/SwingUtilities.html#invokeLater%28java.lang.Runnable%29 –  Sep 12 '11 at 18:02

1 Answers1

5

invokeLater still ends up running the code on the dispatcher thread - just later. The aim of invokeLater is to allow background threads to post work on the event dispatcher thread.

It sounds like you should indeed create another thread - or use a thread pool for the same sort of effect, or SwingWorker for example. Whatever you do, you need to avoid running your slow method on the event dispatcher thread.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194