Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Gotta love code from the internet...oh...</p> <p>The code you have violates the singe thread rules of Swing and thus, is a bad example.</p> <p>You have a number of options with <code>SwingWorker</code>. You could <code>publish</code> the progress and use the <code>process</code> method to update the progress bar or you could use a <code>PropertyChangeListener</code> and monitor progress change events created by calling the <code>setProgress</code> method of the <code>SwingWorker</code></p> <pre><code>import java.awt.BorderLayout; import java.awt.EventQueue; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JProgressBar; import javax.swing.SwingWorker; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; import javax.swing.border.EmptyBorder; public class SwingWorkerProgress { public static void main(String[] args) { new SwingWorkerProgress(); } public SwingWorkerProgress() { EventQueue.invokeLater(new Runnable() { @Override public void run() { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { } JFrame frame = new JFrame("Testing"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLayout(new BorderLayout()); frame.add(new TestPane()); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } }); } public class TestPane extends JPanel { private JProgressBar pbProgress; private JButton start; public TestPane() { setBorder(new EmptyBorder(10, 10, 10, 10)); pbProgress = new JProgressBar(); setLayout(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); gbc.insets = new Insets(4, 4, 4, 4); gbc.gridx = 0; gbc.gridy = 0; add(pbProgress, gbc); start = new JButton("Start"); gbc.gridy++; add(start, gbc); start.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { start.setEnabled(false); ProgressWorker pw = new ProgressWorker(); pw.addPropertyChangeListener(new PropertyChangeListener() { @Override public void propertyChange(PropertyChangeEvent evt) { String name = evt.getPropertyName(); if (name.equals("progress")) { int progress = (int) evt.getNewValue(); pbProgress.setValue(progress); repaint(); } else if (name.equals("state")) { SwingWorker.StateValue state = (SwingWorker.StateValue) evt.getNewValue(); switch (state) { case DONE: start.setEnabled(true); break; } } } }); pw.execute(); } }); } } public class ProgressWorker extends SwingWorker&lt;Object, Object&gt; { @Override protected Object doInBackground() throws Exception { int i = 0; int max = 2000; while (i &lt; max) { i += 10; int progress = Math.round(((float)i / (float)max) * 100f); setProgress(progress); try { Thread.sleep(25); } catch (Exception e) { e.printStackTrace(); } } return null; } } } </code></pre>
 

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