ActionListener on a button with several lines
Heya, to start.. sry that the code i will put aint pasted with the "java code" (showing lines and etc), but after looking around a bit didnt see what to write to make it appear.
I wanted to make a Button that i could define its size and make the several words appear in different lines, from what i searched, a way to do it would be using labbels.
my solution was:
public class MultiLineButton extends JButton {
public MultiLineButton(String label1, String label2) {
this.setLayout(new GridLayout(2, 1));
add(new JLabel(label1));
add(new JLabel(label2));
}
}
using gridlayout so the lines dont appear at the extremes like it would with borderlayout.north/south.
My problem starts on how can i add a actionListner on a button wich text is customized?
the previus situations where i used actionListner was for a JMenuBar where i would type the text shown in the ( e.getActionCommand().equals(xxxx) ) and it activated.
i have seen that you can use Button.getText() to recieve its text, i tried adding on my MultiLineButton:
private String label1;
private String label2;
public String getText() { return label1 + " " + label2; }
doing this changes the appearence of my buttons to:
ex: Add (label1)
null null
Category (label2)
would appreciate if someone could point what im doing wrong/missing or tell me some new stuff i should look into for what i intend to do.
cumps
Re: ActionListener on a button with several lines
One possible solution: give the JButton its own actionCommand via the setActionCommand(String text) method. Another solution: give the button its own anonymous ActionListener.
Re: ActionListener on a button with several lines
thank you for the answer, i wanted to something similar to the first solution you mentioned.. but from what i saw on the api, thought it wasnt possible.. will work on it now.
Re: ActionListener on a button with several lines
You're welcome. The key is that the actionCommand String and the JButton's text are distinct. Yes they're set to the same value if you construct the JButton with a String, but you can easily change the actionCommand property to whatever String you like.