Note that there are some explanatory texts on larger screens.

plurals
  1. POMaking several `JPanel`s change their color one by one and not all together with Java Swing
    text
    copied!<p>I have a <code>JPanel</code> that has several (25) <code>JLabel</code>s on it, using a timer they change their color every 2 seconds (all panels change to the same color), according to a random order, I only see that order because I've added text prints on each change, but the color changes together, even when I've added a delay between each change (in case my eye wasn't able to see the gradual change) and it didn't help, they all changed color together after a longer wait.</p> <p>How do I make them change color one-by-one?</p> <p>The code:</p> <pre><code>public class Billboard extends JFrame{ private JPanel board; private ArrayList&lt;Panel&gt; panels; public Billboard() { super("Billboard"); this.board = new JPanel(); setBounds(100,100,250,160); this.panels = new ArrayList&lt;Panel&gt;(0); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container con=this.getContentPane(); // inherit main frame con.add(board); // add the panel to frame //The timer that is responsible to changing the colors ColorGenerator cg = ColorGenerator.getInstance(); RandomNotifier note = new RandomNotifier(); cg.setNotificator(note); JLabel l; Panel p; for (int i=0; i&lt;25; i++) { // create label l= new JLabel (" "); l.setOpaque(true); // add label to screen board.add(l); // create panel p = new Panel("p" + i,l); // link ColorGenerator to panel cg.addObserver(p); // add panel to panels list panels.add(p); } setVisible(true); // display the frame //starts the timer cg.start(); } public static void main(String args[]) {new Billboard();} } public class Panel implements Observer{ private String _name; private Color _color; private JLabel _label; public Panel (String name, JLabel l) { this._name = name; this._color= new Color(0,0,0); this._label = l; this._label.setBackground(this._color); checkRep(); } //updates the color of the label public void update(Observable o, Object arg) { ColorGenerator cg = (ColorGenerator) o; setColor(cg.getColor()); this._label.setBackground(this.getColor()); System.out.println(this.getName() + " has changed its color."); } private void setColor(Color c) { this._color = c; } private Color getColor () { return this._color; } } public class ColorGenerator extends Observable { private Notifier _notifier; private Color _color; private Timer _timer; private ArrayList&lt;Panel&gt; _observers; //hold the link to the only ColorGenerator private static ColorGenerator _cg = null; public static ColorGenerator getInstance() { if (_cg == null) { _cg = new ColorGenerator(); } return _cg; } protected ColorGenerator () { ActionListener changeColorTask = new ActionListener() { public void actionPerformed(ActionEvent evt) { Random rand_gen = new Random(); _color = new Color (rand_gen.nextInt(256),rand_gen.nextInt(256),rand_gen.nextInt(256)); setChanged(); notifyObservers(); } }; this._color = new Color(0,0,0); this._timer = new Timer(2000, changeColorTask); this._timer.setInitialDelay(0); this._observers = new ArrayList&lt;Panel&gt;(); this._notifier = null; } public void start() { this._timer.start(); } public Color getColor () { return this._color; } @Override public void addObserver(Observer o) { //... } @Override public void deleteObserver(Observer o) { //... } @Override public void deleteObservers() { //... } @Override public int countObservers() { //... } @Override public void notifyObservers(){ if (!hasChanged()) return; if (this._notifier == null) { System .out.println ("Notificator has not set for ColorGenerator, can't notify observers"); return; } this._notifier.notifyAll(this,this._observers); clearChanged(); } public void setNotifier (Notifier n) { if (n == null) return; this._notifier = n; } } </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