开发者

Best way of Changing the background of JButtons

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 actionP开发者_运维百科erformed(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();


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 64x64 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.


You can get a considerable benefit if only visible buttons need to be repainted. In the MVC approach shown below, each button listens to a model that defines it's current state. Updating the model is quite fast compared to repainting. Although startup takes a few seconds, I see updates taking < 10 ms. in the steady-state. It's not as scalable as the flyweight pattern used by JTable, illustrated here, but it may serve.

import java.awt.*;
import java.awt.event.*;
import java.util.Observable;
import java.util.Observer;
import java.util.Random;
import javax.swing.*;

/** @see https://stackoverflow.com/questions/6117908 */
public class UpdateTest {

    private static final int ROW = 64;
    private static final int COL = 64;
    private static final int MAX = COL * ROW;
    private final DataModel model = new DataModel(MAX);

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new UpdateTest().create();
            }
        });
    }

    void create() {
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(ROW, COL));
        for (int i = 0; i < MAX; i++) {
            panel.add(new ViewPanel(model, i));
        }
        Timer timer = new Timer(1000, new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                long start = System.nanoTime();
                model.update();
                System.out.println(
                    (System.nanoTime() - start) / (1000 * 1000));
            }
        });
        JFrame f = new JFrame("JTextTest");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(new JScrollPane(panel), BorderLayout.CENTER);
        f.setPreferredSize(new Dimension(800, 600));
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
        timer.start();
    }

    private static class ViewPanel extends JPanel implements Observer {

        private final JButton item = new JButton();
        private DataModel model;
        private int index;

        public ViewPanel(DataModel model, int i) {
            this.model = model;
            this.index = i;
            this.add(item);
            item.setText(String.valueOf(i));
            item.setOpaque(true);
            item.setBackground(new Color(model.get(index)));
            model.addObserver(this);
        }

        @Override
        public void update(Observable o, Object arg) {
            int value = model.get(index);
            item.setBackground(new Color(value));
        }
    }

    private static class DataModel extends Observable {

        private final Random rnd = new Random();
        private final int[] data;

        public DataModel(int n) {
            data = new int[n];
            fillData();
        }

        public void update() {
            fillData();
            this.setChanged();
            this.notifyObservers();
        }

        public int get(int i) {
            return data[i];
        }

        private void fillData() {
            for (int i = 0; i < data.length; i++) {
                data[i] = rnd.nextInt();
            }
        }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜