| Author |
whats wrong with my code ?
|
naved momin
Ranch Hand
Joined: Jul 03, 2011
Posts: 675
|
|
what i want to do the button's text should change to the text written in actionperformed method ...but how to implement ?
|
The Only way to learn is ...........do!
Visit my blog http://inaved-momin.blogspot.com/
|
 |
Mohamed Sanaulla
Bartender
Joined: Sep 08, 2007
Posts: 2926
|
|
Your ButtonAction doesn't have the reference to the button on which it has to perform the action. One way is to pass the button reference to the ButtonAction so that the same can be accessed in the actionPerformed method.
|
Mohamed Sanaulla | My Blog
|
 |
mike ryan
Ranch Hand
Joined: Aug 08, 2010
Posts: 210
|
|
Hi,
Well for one thing in your ButtonAction method, you will need to find out first if the "Button", is being pressed .You can either set an ActionCommand OR get the source of the current Action.
if(get the source.equals(some source)){
do something;
}
or
if(get some ActionCommand.equals("some ActionCommand")){
do something;
}
Hope that helps a bit .
Mike
|
 |
naved momin
Ranch Hand
Joined: Jul 03, 2011
Posts: 675
|
|
mike ryan wrote:Hi,
Well for one thing in your ButtonAction method, you will need to find out first if the "Button", is being pressed .You can either set an ActionCommand OR get the source of the current Action.
if(get the source.equals(some source)){
do something;
}
or
if(get some ActionCommand.equals("some ActionCommand")){
do something;
}
Hope that helps a bit .
Mike
i didnt understand both of you ,
1. first how can i pass a button to actionperformed method ???
|
 |
Darryl Burke
Bartender
Joined: May 03, 2008
Posts: 4163
|
|
|
Naved, please UseAMeaningfulSubjectLine. "whats wrong with my code ?" isn't meaningful, and I see the majority of your threads have the very same subject line.
|
luck, db
There are no new questions, but there may be new answers.
|
 |
Mohamed Sanaulla
Bartender
Joined: Sep 08, 2007
Posts: 2926
|
|
You are not passing the button reference to the actionPerformed, instead you declare the JButton as the member variable of the ButtonAction class and in turn your actionPerformed will use that reference.
|
 |
mike ryan
Ranch Hand
Joined: Aug 08, 2010
Posts: 210
|
|
|
mabe take a look at this link tutorial
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32654
|
|
mike ryan wrote: . . .You can either set an ActionCommand OR get the source of the current Action. . . .
I believe you should do neither. If you add the Listener to a particualr button, it is linked to that particular button, so neither of those actions is necessary. Nor beneficial. The direct linking is an object-oriented approach.
Source and action commands are not object-oriented.
Never mind that the Java Tutorials uses getSource, the individual class approach NM showed is much better. You jsut ahve to get the reference to the button passed. This is one way to do it:By the way, that class is inappropriately named. It is a Listener, and its name should end with Listener. ButtonTextChangingActionListener, maybe
|
 |
mike ryan
Ranch Hand
Joined: Aug 08, 2010
Posts: 210
|
|
Hi Campbell,,
Sorry that is the way i am being taught in my course, so that is the way i would have done it. I will have to try your way and see how that works.
Mike
|
 |
naved momin
Ranch Hand
Joined: Jul 03, 2011
Posts: 675
|
|
Campbell Ritchie wrote:
mike ryan wrote: . . .You can either set an ActionCommand OR get the source of the current Action. . . .
I believe you should do neither. If you add the Listener to a particualr button, it is linked to that particular button, so neither of those actions is necessary. Nor beneficial. The direct linking is an object-oriented approach.
Source and action commands are not object-oriented.
Never mind that the Java Tutorials uses getSource, the individual class approach NM showed is much better. You jsut ahve to get the reference to the button passed. This is one way to do it: By the way, that class is inappropriately named. It is a Listener, and its name should end with Listener. ButtonTextChangingActionListener, maybe
thanks campbell ....
it helps me learning in this way .....
|
 |
 |
|
|
subject: whats wrong with my code ?
|
|
|