0

In my quest to understand concurrency in Java. I created a program with a GUI that has a JTextArea named display.

There is also a JButton that one clicks to open a secondary frame to play with a JProgressBar (see here).

The ActionListener for the button that opens the new frame includes this code:

SwingWorker<String, Void> openProgressorGUI = 
        new SwingWorker<String, Void>() {

    @Override
    protected String doInBackground() throws Exception {
        ProgressorGUI bp = new ProgressorGUI();
        bp.setVisible(true);
        bp.pack();
        return null;
    }

};
openProgressorGUI.execute();

display.append("You just opened the Progress Bar window”);

When the button is clicked, the secondary frame opens and does what it needs to, but the display in the main GUI doesn't print "You just opened the Progress Bar window".

My understanding is that a SwingWorker opens a new background thread so the event dispatch thread is not interrupted.

Why isn't control continuing past the SwingWorker to finish the code in the ActionListener?

Community
  • 1
  • 1
MayNotBe
  • 2,110
  • 3
  • 32
  • 47
  • Did you read and understand [*this*](http://docs.oracle.com/javase/7/docs/api/javax/swing/package-summary.html#threading) and [*this*](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/dispatch.html)? `doInBackground()` is, as the name suggests, for performing *background* tasks, *not* for manipulating the GUI. – Holger May 02 '14 at 14:38
  • I have indeed read both. I'm confused how it applies to my situation. I (perhaps mistakenly) feel like opening a new frame should not interfere with the current frame. Clearly I'm not understanding the event dispatch thread. – MayNotBe May 02 '14 at 14:48
  • 1
    There is no exception for frames. Everything which belongs the the GUI should be accessed by exactly one thread, the EDT. – Holger May 02 '14 at 15:28

0 Answers0