This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes whats wrong with my code ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "whats wrong with my code ?" Watch "whats wrong with my code ?" New topic
Author

whats wrong with my code ?

naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 688

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
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3064
    
  33

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: 688

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: 4523
    
    5

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
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3064
    
  33

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: 37923
    
  22
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: 688

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 .....
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: whats wrong with my code ?
 
Similar Threads
Button Listener
change the texts to a different one in a JButton each time it is pressed.
How do I pass a frame into another class?
whats wrong with my code ?
function scope?