简体   繁体   中英

Java SwingWorker locks up GUI

I have the following code...

runNow.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {

    final Search s = (Search) node.getUserObject();

    // Swing worker
    SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {
        @Override
        public Void doInBackground() {
            s.run();
            return null;
        }

        @Override
        public void done() {
            window.updateResultsPanel(s.getResults(), s.getName());
         }
    };

    worker.run();

    }
});

This is a popup menu action which should create a new SwingWorker, free up the GUI, do some work, then update the results of the window. However, right now when I click the menu item the GUI becomes locked up until the work completes which is baffling, the whole point of using a SwingWorker is so that won't happen.

Any ideas as to what I'm doing wrong would be greatly appreciated.

-Cody

You should be calling SwingWorker#execute to start the worker, not run

Run is exposed by the Runnable interface, but execute actually schedules the worker to run at some time in the future.

必须将方法SwingWorker.run()更改为SwingWorker.execute() ,它的工作原理就像一个魅力。

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