Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2007

Tip: Looking for answers? Try searching our database.

Setting properties on similar objects simultaneously

Thread view: 
jackroofman@gmail.com - 01 May 2007 19:52 GMT
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[i] = new JButton();
           buttons[i].setBounds(i*25, 5, 20, 20);
           buttons[i].setName(functions[i]);
           buttons[i].setVisible(true);
           buttons[i].addActionListener(this);
           buttons[i].setBorderPainted(false);
           buttons[i].setIcon(new ImageIcon(createImage(new
FilteredImageSource(new

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

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

ImageIcon("buttonbase.png").getImage().getSource(), new
CropImageFilter(i * 20, 40, 20, 20)))));
           add(buttons[i]);
       }
       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[i]);
           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
Tom Hawtin - 01 May 2007 20:11 GMT
> 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
jackroofman@gmail.com - 02 May 2007 19:50 GMT
> I would suggest putting all the common in a method:
>
[quoted text clipped - 10 lines]
>          }
> 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?
Tom Hawtin - 03 May 2007 04:38 GMT
> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.