简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM