Setting properties on similar objects simultaneously

J

jackroofman

I'm writing a program with a wide variety of interfaces. Two of them
are some JButtons and JToggleButtons. The components are so similar
that I'm setting almost identical properties on it, but I have to do
it separately because a JButton is not a JToggleButton. My code is:

for (int i=0; i<5; i++) {
buttons = new JButton();
buttons.setBounds(i*25, 5, 20, 20);
buttons.setName(functions);
buttons.setVisible(true);
buttons.addActionListener(this);
buttons.setBorderPainted(false);
buttons.setIcon(new ImageIcon(createImage(new
FilteredImageSource(new

ImageIcon("buttonbase.png").getImage().getSource(), new
CropImageFilter(i * 20, 0, 20, 20)))));
buttons.setRolloverIcon(new ImageIcon(createImage(new
FilteredImageSource(new

ImageIcon("buttonbase.png").getImage().getSource(), new
CropImageFilter(i * 20, 20, 20, 20)))));
buttons.setPressedIcon(new ImageIcon(createImage(new
FilteredImageSource(new

ImageIcon("buttonbase.png").getImage().getSource(), new
CropImageFilter(i * 20, 40, 20, 20)))));
add(buttons);
}
for (int i=5; i<7; i++) {
toggles[i-5] = new JToggleButton();
toggles[i-5].setBounds(i * 25, 5, 20, 20);
toggles[i-5].setName(functions);
toggles[i-5].setVisible(true);
toggles[i-5].addActionListener(this);
toggles[i-5].setBorderPainted(false);
toggles[i-5].setIcon(new ImageIcon(createImage(new
FilteredImageSource(new

ImageIcon("buttonbase.png").getImage().getSource(), new
CropImageFilter(i * 20, 0, 20, 20)))));
toggles[i-5].setRolloverIcon(new ImageIcon(createImage(new
FilteredImageSource(new

ImageIcon("buttonbase.png").getImage().getSource(), new
CropImageFilter(i * 20, 20, 20, 20)))));
toggles[i-5].setRolloverSelectedIcon(new
ImageIcon(createImage(new FilteredImageSource(new

ImageIcon("buttonbase.png").getImage().getSource(), new
CropImageFilter(i * 20, 40, 20, 20)))));
toggles[i-5].setSelectedIcon(new ImageIcon(createImage(new
FilteredImageSource(new

ImageIcon("buttonbase.png").getImage().getSource(), new
CropImageFilter(i * 20, 60, 20, 20)))));
add(toggles[i-5]);
}

private JButton buttons[] = new JButton[7];
private JToggleButton toggles[] = new JToggleButton[2];

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?

Thanks,
Stephen
 
T

Tom Hawtin

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 = 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
 
J

jackroofman

I would suggest putting all the common in a method:

private void initButton(JButton button, int index) {
button.setBounds(index*25, 5, 20, 20);
...
}

for (int i=5; i<7; i++) {
JToggleButton button = new JToggleButton();
initButton(button, i);
... JToggleButton specific stuff...
toggles[i-5] = button;
}
Tom Hawtin

I've tried that method before, but the problem I keep getting is that
I can't give it a JToggleButton if it's expecting a JButton, and any
attempt I make to differentiate, like with an Object and a getClass()
if/else results in variables not being available and the like.

I need some way to work with a JButton and JToggleButton at the same
time. Is there some creative use of classes and typecasting that might
allow this?
 
T

Tom Hawtin

I need some way to work with a JButton and JToggleButton at the same
time. Is there some creative use of classes and typecasting that might
allow this?

The obvious common type is javax.swing.AbstractButton.

I'm not sure if there are any method signatures that are common between
JButton and JToggleButton, but not present in AbstractButton. There are
two (reasonable) general solutions to deal with such methods.


Introduce an interface, and extend each class to implement it:

interface CommonAbstractButton {
void setCommon(SomeType value);
}

class CommonButton extends JButton implements CommonAbstractButton {
CommonButton() {
super();
}
... other constructors ...
}

class CommonToggleButton extends JToggleButton implements
CommonAbstractButton {
...
}


Or, for more flexibility at the cost of more work, add a layer of
indirection:

abstract class CommonAbstractButton {
private final AbstractButton target;
protected CommonAbstractButton(AbstractButton target) {
if (target == null) {
throw new NullPointerException();
}
this.target = target;
}
public void setLabel(String label) {
target.setLabel(label);
}
public abstract void setCommon(SomeType value);
}
class CommonButton extends CommonAbstractButton {
private final JButton target;
public CommonButton(JButton target) {
super(target);
this.target = target;
}
public void setCommon(SomeType value) {
target.setCommon(value);
}
}

Tom Hawtin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top