Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Some General Rules</strong></p> <ul> <li>Swing is not thread safe, you should only ever update UI components from within the context of the Event Dispatching Thread.</li> <li>You do not control the paint process, the repaint manager does. You can request updates to occur by calling <code>repaint</code>, but you should never call <code>update</code> and <code>paint</code> directly when trying to update the display.</li> <li>The <code>Graphics</code> context used by the paint sub system is a shared resource and is not guaranteed to be the same between paint cycles, you should never maintain a reference to it. You should also not rely on the results from <code>JComponent#getGraphics</code> this method is capable of returning null.</li> </ul> <p><strong>An Example Solution</strong></p> <p>You have a number of options, depending on what you want to ultimately achieve.</p> <p>You could use a <code>SwingWorker</code>, but given the fact that all your going to is enter an infinite loop and it would easier to use <code>SwingUtilities#invokeLater</code> then actually use the <code>publish</code> method, this approach would actually be more work.</p> <p>You could also use a <code>Thread</code>, but you'd end up with the same problems as using a <code>SwingWorker</code></p> <p>The simpliset solution, for what you're presented, is actually a <code>javax.swing.Timer</code></p> <pre><code>public class Blinky { public static void main(String[] args) { new Blinky(); } public Blinky() { EventQueue.invokeLater(new Runnable() { @Override public void run() { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { } JFrame frame = new JFrame("Test"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLayout(new BorderLayout()); frame.add(new BlinkyPane()); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } }); } protected class BlinkyPane extends JPanel { private JLabel blinkyLabel; private boolean blink = false; public BlinkyPane() { setLayout(new GridBagLayout()); blinkyLabel = new JLabel("I'm blinking here"); blinkyLabel.setBackground(Color.RED); add(blinkyLabel); Timer timer = new Timer(250, new ActionListener() { @Override public void actionPerformed(ActionEvent e) { blink = !blink; if (blink) { blinkyLabel.setForeground(Color.YELLOW); } else { blinkyLabel.setForeground(Color.BLACK); } blinkyLabel.setOpaque(blink); repaint(); } }); timer.setRepeats(true); timer.setCoalesce(true); timer.start(); } @Override public Dimension getPreferredSize() { return new Dimension(200, 100); } } } </code></pre> <p>You can take a look at <a href="http://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html" rel="nofollow">Swing Timer</a> and <a href="http://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html" rel="nofollow">Concurrency in Swing</a> for more info</p>
    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. 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.
 

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