JButtons & Action Listeners

Hi,I was wondering how can I make a button switch its label when you click on it?eg. a button on a screen is labeled "1" and by clicking on it, the label changes to "2".Any suggestions would be great.thanx
[240 byte] By [cerebroa] at [2007-10-2 1:54:20]
# 1
add an [url= http://java.sun.com/docs/books/tutorial/uiswing/events/actionlistener.html]ActionListener[/url] to your JButton and use the setLabel method to change the label according to your needs
Torajiroua at 2007-7-15 19:35:32 > top of Java-index,Java Essentials,New To Java...
# 2
It's an array of buttons so how do i know which button to adress.i tried to use this code but it didnt work:public void actionPerformed (ActionEvent event){event.getSource().setLabel ("text");}how would i get this to work?
cerebroa at 2007-7-15 19:35:32 > top of Java-index,Java Essentials,New To Java...
# 3
you can set the action commands for each button. and then compare if the button's action command to the set of available action commands. I'll look for the api
tiners506a at 2007-7-15 19:35:32 > top of Java-index,Java Essentials,New To Java...
# 4

//create an inner class for your action listener

//Then do similar to whats below

/* Your button.addActionListener(

new innerclass action listener(this));

*/

//then create an action performed method that the inner class

//calls. And you can use the Action Event to get the action comand.

btn_actionPerformed(ActionEvent e)

{

String action = e.getActionCommand();

if(action.equals(this.buttons[i].getActionCommand()))

{

whatYouWantToHappenWhenButtonPushed();

}

}

I didnt make this solution very pretty you'll have to try it out and look up the API's probably.

tiners506a at 2007-7-15 19:35:32 > top of Java-index,Java Essentials,New To Java...
# 5

look at the docs

[url]http://java.sun.com/j2se/1.4.2/docs/api/java/util/EventObject.html#getSource()[url]

this method returns an Object

make sure you know what this Object is.

if your listener is registered to only buttons then it should be easy.

think about using this method

[url]http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/AbstractButton.html#setText(java.lang.String)[/url]

but do you understand that you need to do a cast?

walken16a at 2007-7-15 19:35:32 > top of Java-index,Java Essentials,New To Java...
# 6

crud, hope this looks better

look at the docs

[url]http://java.sun.com/j2se/1.4.2/docs/api/java/util/EventObject.html#getSource()[/url]

this method returns an Object

make sure you know what this Object is.

if your listener is registered to only buttons then it should be easy.

think about using this method

[url]http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/AbstractButton.html#setText(java.lang.String)[/url]

but do you understand that you need to do a cast?

walken16a at 2007-7-15 19:35:32 > top of Java-index,Java Essentials,New To Java...
# 7
So would i cast the event into a button, and then use the setText() method?how would i do such a cast?sorry i'm new.
cerebroa at 2007-7-15 19:35:32 > top of Java-index,Java Essentials,New To Java...
# 8
((JButton)e.getSource()).setText("text");
walken16a at 2007-7-15 19:35:32 > top of Java-index,Java Essentials,New To Java...