Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The problem is, you're trying to update the progress within the context of the Event Dispatching Thread.</p> <p>This basically means that while you are in you loop, the EDT is unable to process any paint request you are making.</p> <p>What you need to do is some how offload the work to a separate thread and update the progress bar as needed. The problem with this, is you should never update the UI from any thread other then the EDT.</p> <p>But don't despair, you have a number of options, the best is using a <a href="http://docs.oracle.com/javase/tutorial/uiswing/concurrency/worker.html" rel="nofollow noreferrer">Swing Worker</a></p> <p><strong>Updated with example</strong></p> <p><img src="https://i.stack.imgur.com/h9HLH.png" alt="enter image description here"></p> <pre><code>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>
    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