How to use SwingWorker in Java?

Related to my previous question: rename a call from another class to Java?

I am new to Java and I have read some SwingWorker tutorials. However, I'm not sure how to implement it with the sample code that I gave in the previous question.

Can someone explain how to use SwingWorker regarding my piece of code and / or point me to a decent tutorial? I looked, but I'm not sure I understand.

+40
java animation swingworker
Apr 23 '09 at 15:28
source share
2 answers

Typically, SwingWorker is used to perform lengthy tasks in Swing.

Performing lengthy tasks in the event dispatch (EDT) stream can lead to GUI blocking, so one of the things that were done is to use SwingUtilities.invokeLater and invokeAndWait , which supports a graphical interface that prioritizes other AWT events before running the task ( to form a Runnable ).

However, the problem with SwingUtilities is that it did not allow you to return data from a Runnable run to the original method. This is what SwingWorker is for addressing.

The Java tutorial has a SwingWorker section.

Here is an example where a SwingWorker used to perform a time-consuming task in a separate thread and displays a message box in a second with a response.

First of all, the SwingWorker class will be created:

 class AnswerWorker extends SwingWorker<Integer, Integer> { protected Integer doInBackground() throws Exception { // Do a time-consuming task. Thread.sleep(1000); return 42; } protected void done() { try { JOptionPane.showMessageDialog(f, get()); } catch (Exception e) { e.printStackTrace(); } } } 

The return type of the doInBackground and get methods is specified as the first type of SwingWorker , and the second type is the type returned for publish and process methods that are not used in this example.

Then, to call SwingWorker , the execute method is called. In this example, we will connect an ActionListener to a JButton to execute AnswerWorker :

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

The above button can be added to the JFrame and pressed to get the message box in a second. You can use the following to initialize the GUI for a Swing application:

 private void makeGUI() { final JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.getContentPane().setLayout(new FlowLayout()); // include: "class AnswerWorker" code here. // include: "JButton" b code here. f.getContentPane().add(b); f.getContentPane().add(new JButton("Nothing")); f.pack(); f.setVisible(true); } 

After launching the application there will be two buttons. One is marked as "Answer!". and other "Nothing." When you click β€œAnswer!”, Nothing will happen at first, but pressing the β€œNothing” button will work and demonstrate that the graphical interface is responsive.

And, after a second, the result of AnswerWorker appears in the message box.

+101
Apr 23 '09 at 15:37
source share

I accept

Running long-running tasks in the event dispatch (EDT) thread can block the GUI.

Do not agree:

therefore, one of the things that were done is to use SwingUtilities.invokeLater and invokeAndWait, which supports a graphical interface.

invokeLater is still running EDT code and may freeze your interface! Try the following:

 SwingUtilities.invokeLater(new Runnable() { @Override public void run() { try { Thread.sleep(100000); } catch (InterruptedException e) { e.printStackTrace(); } } }); 

At least I can not move the mouse as soon as I click the button that launches actionPerformed with the above code. Did I miss something?

+7
Jul 12 '13 at 13:42 on
source share



All Articles