Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

whats wrong with my code ?

 
naved momin
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what i want to do the button's text should change to the text written in actionperformed method ...but how to implement ?

 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
mike ryan
Ranch Hand
Posts: 210
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5148
11
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 210
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mabe take a look at this link tutorial
 
Campbell Ritchie
Sheriff
Pie
Posts: 50211
79
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 210
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 .....
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic