Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7399933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T04:07:33+00:00 2026-05-29T04:07:33+00:00

I have a JPanel that has several (25) JLabel s on it, using a

  • 0

I have a JPanel that has several (25) JLabels 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.

How do I make them change color one-by-one?

The code:

public class Billboard extends JFrame{


    private JPanel board;
    private ArrayList<Panel> panels; 


    public Billboard()
    {
        super("Billboard");
        this.board = new JPanel();
        setBounds(100,100,250,160);
        this.panels = new ArrayList<Panel>(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<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<Panel> _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<Panel>();
        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;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T04:07:34+00:00Added an answer on May 29, 2026 at 4:07 am

    You need to call repaint() on the JFrame or JPanel when you want the colors to change.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Right, I have a JTabbedPane that has a JPanel that contains a JLabel and
I have a Graphics object of JPanel and that is working fine: import java.awt.Color;
I have a JScrollpane that has a JPanel on the inside (and the panel
I have a JPanel added to a JViewport , and the panel has several
I have a JPanel that encapsulates two JPanels, one on top of the other.
How can i draw 2d in a JPanel that i have on my GUI?
I have a JFrame that contains a display JPanel with JTextField and a control
I have a window that has two layers: a static background and a foreground
basically, I have a program that has a class to create a basic GUI,
Basically I have a GUI that inherits from the JFrame class and has its

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.