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 5949881
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:18:50+00:00 2026-05-22T17:18:50+00:00

Right now i change the background color of a button by using button.setBackground(Color.WHITE); That

  • 0

Right now i change the background color of a button by using

button.setBackground(Color.WHITE);

That being an example.

But when i have a massive grid out of jbuttons (1000+), just running a for loop to change every buttons background is very, very slow. You can see the grid slowly turning white, box by box. I really don’t want this

Is there a better way of changing every JButton on the grid to the same color at the same time?

This is how i am making the grid, the numbers used are only for example…

grid = new JPanel(new GridLayout(64, 64, 0, 0));

That’s 4096 buttons, takes about 30+ seconds to change every button to the same color.

Edit 1: I need the buttons to be clickable, like when i click a button it turns blue for example. when all of the buttons are clicked, change the color of every button to white. Right now i have that working fine, but it is just slow to change the color of every button.

Edit 2: this is how i am changing the buttons:

    new javax.swing.Timer(300, new ActionListener() {
        int counter = 0;
        public void actionPerformed(ActionEvent e) {
            if (counter >= counterMax) {
                ((Timer) e.getSource()).stop();
            }
            Color bckgrndColor = (counter % 2 == 0) ? flashColor : Color.white;
            for (JButton button : gridButton) {
                button.setBackground(bckgrndColor);
            }
            counter++;
        }
    }).start();
  • 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-22T17:18:50+00:00Added an answer on May 22, 2026 at 5:18 pm

    The fact that you see the boxes being repainted individually indicates that either double buffering is turned off, or that the paint code in the button UI makes use of paintImmediately().

    I tested your setup with 64×64 JButtons, an made sure that all UI operations were executed in the EDT (Event Dispatch Thread). I can confirm the effect you saw, changing the background of all buttons took about 1200 ms, with every box repainted immediately.
    You can bypass the immediate repaints by setting the grid to non-visible before, and to visible after you changed the backgrounds:

    grid.setVisible(false);
    for (Component comp : grid.getComponents()) {
       comp.setBackground(color);
    }
    grid.setVisible(true);
    

    This caused the grid to do only one repaint, and reduced the time to ~300ms (factor 4).

    This is still too slow for frequent updates, so you’re better off with a custom component which draws the grid, or a flyweight container (what trashgod suggested in the comment to your question) if you want allow the grid cells to be arbitrary components.

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

Sidebar

Related Questions

Right now I am trying to change the background color of a org.eclipse.swt.widgets.Button with
I would like to change my default route values. Right now, I have: routes.MapRoute(
I have a splitter that works fine. Now the client wants to change the
Right now, the background I get is a grey. I want to change it
Right now, the white background is killing my eyes, as it's a looong dark
Right now to populate my GridViews I'm using a extended BaseAdapter class on each
Right now i am using the ArrayList for My ListView and there is a
Right now I have a few folders (img, css, js, and ico) in the
Right now I can call a method using ajax (:remote=> 'true') at awisprotect_path by
Right now, I am using the ALAssetsLibrary to gather the photos from ALAssetsGroupSavedPhotos and

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.