Setting properties on similar objects simultaneously

Discussion in 'Java' started by jackroofman@gmail.com, May 1, 2007.

  1. Guest

    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
     
    , May 1, 2007
    #1
    1. Advertising

  2. Tom Hawtin Guest

    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 = 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
     
    Tom Hawtin, May 1, 2007
    #2
    1. Advertising

  3. Guest

    On May 1, 2:11 pm, Tom Hawtin <> wrote:
    > 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?
     
    , May 2, 2007
    #3
  4. Tom Hawtin Guest

    wrote:
    >
    > 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
     
    Tom Hawtin, May 3, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Madhur Ahuja
    Replies:
    2
    Views:
    1,269
    Andrew Thompson
    Aug 4, 2004
  2. Mark
    Replies:
    3
    Views:
    329
    Thomas Matthews
    Nov 6, 2003
  3. Victor Porton
    Replies:
    1
    Views:
    702
    Steven T. Hatton
    Aug 29, 2004
  4. Wardie
    Replies:
    4
    Views:
    490
    Wardie
    Nov 8, 2006
  5. dot c.
    Replies:
    1
    Views:
    112
    elise huard
    Apr 29, 2011
Loading...

Share This Page