JButton background image
Asked Answered
F

3

5

Hi i am trying to implement Action listener for JButton and code look like following:

ImageIcon imageForOne = new ImageIcon(getClass().getResource("resources//one.png"));
one = new JButton("",imageForOne);
one.setPreferredSize( new Dimension(78, 76));
one.addActionListener(myButtonHandler);

Using the above JButton it looks fineSee the image below for button 1

When i add specific value to button for e.g.

ImageIcon imageForOne = new ImageIcon(getClass().getResource("resources//one.png"));
//Check this
one = new JButton("one",imageForOne);
one.setPreferredSize( new Dimension(78, 76));
one.addActionListener(myButtonHandler);

It look like the following image

Check button 1

Is there any way i can avoid this and set the value too.

Thanks for your help in advance.

Foregut answered 14/6, 2013 at 14:1 Comment(3)
Why do you want to set the label if you don't want to display it anyway? (I fear you want to handle the click based on the button name, but that's not the way to go.)Subjective
@Heuster i want to get that value and use it on action listener. I have different code for action listener too if you want i can post that too. thanksForegut
I agree with Heuster. Instead of using the JButton label text to modify the action, create a custom ActionListener for each button, and embed the button-specific value in the ActionListener. The label text is for graphical display purposes, and is not intended for use as an object handle.Jepum
E
5

Personally, I would be using the Action API.

It will allow you defined a hierarchy of action commands (if that's what you want) as well as define self contained response to the commands.

You could...

public class OneAction extends AbstractAction {
    public OneAction() {
        ImageIcon imageForOne = new ImageIcon(getClass().getResource("resources//one.png"));
        putValue(LARGE_ICON_KEY, imageForOne);
    }

    public void actionPerfomed(ActionEvent evt) {
        // Action for button 1
    }
}

Then you would simply use with your button...

one = new JButton(new OneAction());
one.setPreferredSize( new Dimension(78, 76));

For example...

Eck answered 15/6, 2013 at 0:46 Comment(0)
S
4

Instead of determining the button clicked in the action listener, I would use an adapter pattern:

one.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent arg0) {
        handleClick("one");
    }        
});

where handleClick can still be the handler for all your buttons.

Subjective answered 14/6, 2013 at 14:13 Comment(0)
S
3

i want to get that value and use it on action listener.

You use the action command for this:

one.setActionCommand("1");

However it is better to use the actual text that you want to insert into your display component. Then you can share the ActionListener on all you buttons by using code like:

ActionListener clicked = new ActionListener() 
{
    @Override
    public void actionPerformed(ActionEvent e) {
        String text = e.getActionCommand()
        // displayComponent.appendText(text);
    }        
};

one.addActionListener(clicked);
two.addActionListener(clicked);
Syncopation answered 14/6, 2013 at 15:2 Comment(1)
Thanks mate u are the legend.Foregut

© 2022 - 2024 — McMap. All rights reserved.