Re: Setting properties on similar objects simultaneously
jackroofman@gmail.com wrote:
As you can see, there's only one unique line in the JButton part and
two unique ones in the JToggleButton part. There must be a way for me
to use ONE loop to do all the common ones and then differentiate
between the two objects for their unique properties. I haven't been
able to find any way to do this, however. Any ideas?
I would suggest putting all the common in a method:
private void initButton(JButton button, int index) {
button.setBounds(index*25, 5, 20, 20);
...
}
You still have two loops:
for (int i=0; i<5; i++) {
JButton button = new JButton();
initButton(button, i);
... JButton specific stuff...
buttons[i] = button;
}
for (int i=5; i<7; i++) {
JToggleButton button = new JToggleButton();
initButton(button, i);
... JToggleButton specific stuff...
toggles[i-5] = button;
}
(I would also use List<> instead of arrays, and not duplicate my constants.)
You could always have two loops to create the buttons, then use a third
loop to do all the common stuff. Either creating an array/List of all
the buttons, or passing the arrays/Lists to a method.
Tom Hawtin