This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Swing / AWT / SWT and the fly likes Find source of button click Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Find source of button click" Watch "Find source of button click" New topic
Author

Find source of button click

Jysh Reey
Greenhorn

Joined: Oct 10, 2011
Posts: 16
i have created an Image button and didnt know how to identify the button clicks by the user..
when i use getSource() method am receiving exception...

If it is a normal button we'll be checking the button text...If it is a image button without any text what should be checked to determine the button clicked..? am confused...!
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4369
    
    8

In my opinion, the best approach is to add separate event listeners to each button. That way you don't have to check the button text or anything else about it - you already know which button it is.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> how to identify the button clicks by the user.

one way might be to set the button's name,
setName() and then getName()
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10268
    
    8

Welcome to the Ranch.

Jysh Reey wrote:..hav...kno ..wen......norml...shud

Please UseRealWords
Also Swings!=Swing SwingIsAProperNoun


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Jysh Reey
Greenhorn

Joined: Oct 10, 2011
Posts: 16
Sorry... i changed it in the posts...

Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10268
    
    8

Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1076
    
  10

Jysh Reey wrote:when i use getSource() method am receiving exception...

what exception? how are you using the getSource()?
post a SSCCE to get precise help.


Ranga.
SCJP 1.4, OCMJEA/SCEA 5.0.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19672
    
  18

Jysh Reey, please UseAMeaningfulSubjectLine next time. 90% of the posts in this forum is about Swing, and I dare say that 100% is about Java. I've modified it to something more descriptive.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jysh Reey
Greenhorn

Joined: Oct 10, 2011
Posts: 16
@Rob Spoor: Sorry and thank you....

@Ranganadhan: here it is...
please read it with patience..


this is a code in which i tried the image button action seperately...and it works...
but my actual code is as follows...

here i have tried the event handling for combo box before the button event handling..
so when i try for determining the button pressed it says am comparing a combo box event with a button...
how to turn it right? please help me....
Jysh Reey
Greenhorn

Joined: Oct 10, 2011
Posts: 16
@Mattew Brown: i can get what you are saying...but i don't know how to implement it exactly in my code...because i want them all in a single class... saw some examples in net..they have implemented each actionPerformed() in separate classes...
Jysh Reey
Greenhorn

Joined: Oct 10, 2011
Posts: 16
thank you so much for all of your responses... .
got the answer...

special thanks to Mattew Brown..
who ever come across this issue please watch this vedio --> http://www.youtube.com/watch?feature=player_detailpage&v=IocSF-aYdt8

Dis is my modified code now..


Actually i posted my query in 3 forums and honestly i got proper responses just here..
My project is in the verge of completion...
My sincere thanks to JavaRanch and all the active members...
will be in touch..
mike ryan
Ranch Hand

Joined: Aug 08, 2010
Posts: 210

Jysh ,

Please paste your code in between Code tags, while you are in the post reply edit page , click the Code button up above, then paste your code in between those tags.It is then much easier to read .
Jysh Reey
Greenhorn

Joined: Oct 10, 2011
Posts: 16
sorry i didn't notice that option,,...done now....
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4369
    
    8

Jysh Reey wrote:special thanks to Mattew Brown..

You're welcome! I think that's much better - it's a much more object-oriented approach to have a separate class to respond to each event you've got. Your initial approach of making the whole form into an ActionListener is unfortunately commonly used in examples, but in my opinion it's usually a poor design.

One thing I'd definitely recomment, though, is changing the names of your inner classes to make them more meaningful. For instance, change them to NextActionListener and ExitActionListener.

(In the code you've posted you've also added the ev event handler to both buttons, rather than ev to exit and ae to next, which won't be what you want).
Jysh Reey
Greenhorn

Joined: Oct 10, 2011
Posts: 16
Sure...will keep that in mind while coming across such situations any more..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Find source of button click