I have a bunch of buttons in my JToolBar, and I set some of them to be disabled or enabled depending on the state of my application. I find when I am updating a number of buttons at once that they are not all repainted at the same time. I want to ensure that when I set a number of buttons to be disabled/enabled, that they all change state at the same time.
Below is a small test that demonstrates the problem. (It needs a file a.png in the current directory to use as a button icon.) When you run it, a toolbar with 10 buttons is shown. Pressing Enter at the terminal will toggle the disabled state of all of the buttons. On my machine at least, each time I do this the buttons are repainted in a seemingly random order, and not all at once.
It seems like double buffering might solve the problem, although the first thing I tried (setting double buffering on the JToolBar) didn't seem to affect anything.
Thanks,
Cameron
import java.awt.*;
import javax.swing.*;
import java.io.*;
import java.util.*;
public class Test {
public static void main(String[] args) throws IOException {
final JButton[] bs = new JButton[10];
EventQueue.invokeLater(new Runnable() {
public void run() {
JFrame f = new JFrame("test");
JToolBar t = new JToolBar();
f.getContentPane().add(t);
for (int i = 0; i < bs.length; i++) {
bs[i] = new JButton(new ImageIcon("a.png"));
t.add(bs[i]);
}
f.pack();
f.setVisible(true);
}
});
BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
for (;;) {
r.readLine();
EventQueue.invokeLater(new Runnable() {
public void run() {
for (JButton b : bs) {
b.setEnabled(!b.isEnabled());
}
}
});
}
}
}