0

I am very new to Swing.

I have

itemActionButton.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent arg) {
        itemAction();
    }
});

But when the button is clicked, instead of running this action on another thread, I would like the parent's form's thread to wait until it the action is completed before refreshing, allowing additional clicks, etc.

How can I do this?

Paul Draper
  • 78,542
  • 46
  • 206
  • 285
  • 1
    I thought you were. Running the action from inside the actionPerformed will continue to block the EDT. You could disable the e button (on the click), run the action in a SwingWorker and rentable the button from the SwingWorker's done method – MadProgrammer Apr 06 '13 at 02:09
  • Paul can you see here: http://stackoverflow.com/questions/8083768/stop-cancel-swingworker-thread – Gere Apr 06 '13 at 02:11
  • As has been pointed out, I am running in the main thread here. It turns out that I am calling `itemAction()` indirectly from a TimerTask elsewhere. To get `itemAction()` to run on the main thread, I had to use `SwingUtilities.invokeLater()`. – Paul Draper Apr 12 '13 at 18:26

1 Answers1

1

Code in the ActionListener executes on the EDT, which prevents the GUI from repainting and responding to other events.

If you have a long running task and you don't want to block the EDT then you need to use another Thread.

Read the section from the Swing tutorial on Concurrency for more information and a solution by using a SwingWorker.

camickr
  • 321,443
  • 19
  • 166
  • 288