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 fine
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
Is there any way i can avoid this and set the value too.
Thanks for your help in advance.
You use the action command for this:
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:
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...
Then you would simply use with your button...
For example...
Instead of determining the button clicked in the action listener, I would use an adapter pattern:
where
handleClick
can still be the handler for all your buttons.