Paul Draper
Paul Draper

Reputation: 83353

How to make button click block in Java Swing?

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?

Upvotes: 0

Views: 527

Answers (1)

camickr
camickr

Reputation: 324157

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.

Upvotes: 1

Related Questions