Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Use a <a href="http://docs.oracle.com/javase/7/docs/api/javax/swing/SwingWorker.html" rel="nofollow noreferrer"><code>SwingWorker</code></a> to split UI-update and long running tasks.</p> <p>Take a few minutes to read the end of the <a href="https://stackoverflow.com/tags/swing/info">Swing tag wiki</a> and follow the provided links.</p> <p>Here is a small example of such code:</p> <pre><code>import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.List; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JTextField; import javax.swing.SwingUtilities; import javax.swing.SwingWorker; public class TestSwingWorker { private JTextField progressTextField; protected void initUI() { final JFrame frame = new JFrame(); frame.setTitle(TestSwingWorker.class.getSimpleName()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JButton button = new JButton("Clik me to start work"); button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { doWork(); } }); progressTextField = new JTextField(25); progressTextField.setEditable(false); frame.add(progressTextField, BorderLayout.NORTH); frame.add(button, BorderLayout.SOUTH); frame.pack(); frame.setVisible(true); } protected void doWork() { SwingWorker&lt;Void, Integer&gt; worker = new SwingWorker&lt;Void, Integer&gt;() { @Override protected Void doInBackground() throws Exception { for (int i = 0; i &lt; 100; i++) { // Simulates work Thread.sleep(10); publish(i); } return null; } @Override protected void process(List&lt;Integer&gt; chunks) { progressTextField.setText(chunks.get(chunks.size() - 1).toString()); } @Override protected void done() { progressTextField.setText("Done"); } }; worker.execute(); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { new TestSwingWorker().initUI(); } }); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload