1

The code within the run() method is not being executed. Can anyone tell me why?

startButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                if(creator == null) {
                    String[] args = getArguments();
                    try {
                        writeSettingsFile(args);
                    } catch(IOException io) {}

                    consolePanel.addLine("Starting...");
                    bar.setIndeterminate(true);
                    try {
                        SwingUtilities.invokeAndWait(new Runnable() {

                            public void run() {
                                MessageConsole console = new MessageConsole(getConsole().getTextPane(), self, null);
                                console.redirectOut(new Color(240, 240, 240), null);
                                console.redirectErr(Color.RED, null);
                                console.setMessageLines(consolePanel.getHeight() / 17);
                                try {
                                    SomeApp.main(getArguments());
                                } catch (Exception ex) {
                                    Logger.getLogger(OSXWorldPanel.class.getName()).log(Level.SEVERE, null, ex);
                                }
                            }
                        });
                    } catch (InterruptedException ex) {
                        Logger.getLogger(OSXWorldPanel.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (InvocationTargetException ex) {
                        Logger.getLogger(OSXWorldPanel.class.getName()).log(Level.SEVERE, null, ex);
                    }

                }
            }
        });

Throws Error:

Exception in thread "AWT-EventQueue-0" java.lang.Error: Cannot call invokeAndWait from the event dispatcher thread
    at java.awt.EventQueue.invokeAndWait(EventQueue.java:1017)
    at javax.swing.SwingUtilities.invokeAndWait(SwingUtilities.java:1320)
Jakir00
  • 2,023
  • 4
  • 20
  • 32
  • Are you sure it is not? Have you put some System.out.println()s in? – jzd Jan 07 '11 at 20:37
  • I am sure. When I press the button the run() method is not called. Even with println's it will display nothing. – Jakir00 Jan 07 '11 at 20:41
  • So, where does it stop ? is actionPerformed called at all ? is the creator == null reached and evaluating to true ? etc. – nos Jan 07 '11 at 20:43
  • EDIT: I found an error being thrown – Jakir00 Jan 07 '11 at 20:57
  • Jacob, you are in the event dispaching thead (EDT). You don't need/have to use invokeAndWait. Call your code directly without the invokeAndWait and runnable. – lujop Jan 07 '11 at 21:03

1 Answers1

3

You don't have to call invokeAndWait if you are in the EDT. And in your example it seems that you are in the EDT.

lujop
  • 13,504
  • 9
  • 62
  • 95
  • How do I get around this? I WAS using a Thread, then I used SwingWorker but both of them lock up my GUI... Take into account that `SomeApp.main(getArguments());` is a very heavy process. – Jakir00 Jan 07 '11 at 21:02
  • Start a normal thread and do the job in that thread and when you finish the job call the invokeAndWait inside the worker thread and put in it the code to update the UI. – lujop Jan 07 '11 at 21:06