• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Find source of button click

 
Jysh Reey
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> 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
Posts: 11196
15
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

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

Please UseRealWords
Also Swings!=Swing SwingIsAProperNoun
 
Jysh Reey
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry... i changed it in the posts...

 
Maneesh Godbole
Saloon Keeper
Posts: 11196
15
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
 
Ranganathan Kaliyur Mannar
Bartender
Posts: 1101
10
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jysh Reey
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 210
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry i didn't notice that option,,...done now....
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure...will keep that in mind while coming across such situations any more..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic