Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Swing is a single threaded framework, that is, all interactions and modifications to the UI are expected to occur within the context of the Event Dispatching Thread.</p> <p>The EDT is responsible for, amongst other things, processing repaint requests.</p> <p>Anything that stops the EDT from running (like <code>Thread.sleep</code> and looping for a long time) will prevent it from processing the events, virtually "hanging" your application until it becomes unblocked...</p> <p>Now, I tried to decipher what you program was trying to do...but failed, so instead...I made a pretty counter...</p> <p><img src="https://i.stack.imgur.com/G1SPR.gif" alt="enter image description here"></p> <pre><code>import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.EventQueue; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Random; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.Timer; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; public class TickOver { public static void main(String[] args) { new TickOver(); } public TickOver() { 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 JTextField field; private JButton button; private int tick; private Timer timer; public TestPane() { field = new JTextField(10); field.setEditable(false); button = new JButton("Start"); button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { button.setEnabled(false); tick = 0; timer.start(); } }); timer = new Timer(1000, new ActionListener() { @Override public void actionPerformed(ActionEvent e) { field.setText(Integer.toString(++tick)); if (tick &gt; 4) { timer.stop(); button.setEnabled(true); } } }); timer.setInitialDelay(0); setLayout(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); gbc.gridwidth = GridBagConstraints.REMAINDER; add(field, gbc); add(button, gbc); } } } </code></pre> <p>Take a close look at <a href="http://docs.oracle.com/javase/tutorial/uiswing/concurrency/" rel="nofollow noreferrer">Concurrency in Swing</a> for more details...</p>
 

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