Home > Java > javaTutorial > How Can SwingWorker Prevent GUI Freezes in Java?

How Can SwingWorker Prevent GUI Freezes in Java?

Susan Sarandon
Release: 2024-12-22 15:38:14
Original
461 people have browsed it

How Can SwingWorker Prevent GUI Freezes in Java?

Using SwingWorker in Java

Introduction

Long-running tasks executed on the Event Dispatch Thread can freeze the GUI. This issue can be addressed by using SwingWorker, which supports the execution of such tasks on a separate thread.

How to Use SwingWorker

  1. Define a subclass of SwingWorker: The subclass should define a doInBackground method, where the long-running task is executed, and a done method, which will be called when the task is complete.
  2. Execute the SwingWorker: The execute method is used to start the SwingWorker on a separate thread. You can link this to an event, such as a button click.
  3. Handle the result: The get method can be used to retrieve the result of the task after it has finished. This can be done in the done method or in the event handler that started the SwingWorker.

Example

In this example, a SwingWorker is used to perform a time-consuming task and display a message box with the result one second later:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class SwingWorkerExample {

    private static void makeGUI() {
        final JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.getContentPane().setLayout(new FlowLayout());

        class AnswerWorker extends SwingWorker<Integer, Integer> {
            protected Integer doInBackground() throws Exception {
                Thread.sleep(1000);
                return 42;
            }

            protected void done() {
                JOptionPane.showMessageDialog(f, get());
            }
        }

        JButton b = new JButton("Answer!");
        b.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                new AnswerWorker().execute();
            }
        });

        f.getContentPane().add(b);
        f.getContentPane().add(new JButton("Nothing"));
        f.pack();
        f.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(SwingWorkerExample::makeGUI);
    }
}
Copy after login

The above is the detailed content of How Can SwingWorker Prevent GUI Freezes in Java?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template