Win a copy of Succeeding with AI this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Junilu Lacar
Sheriffs:
  • Tim Cooke
  • Jeanne Boyarsky
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • fred rosenberger
  • Frits Walraven

Find source of button click

 
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...!
 
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.
 
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()
 
Bartender
Posts: 11445
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • 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
Bartender
Posts: 11445
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
 
Bartender
Posts: 1104
10
Netbeans IDE Java 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.
 
Sheriff
Posts: 21922
106
Eclipse IDE Spring VI Editor Chrome Java Ubuntu 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..
 
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..
 
Can't .... do .... plaid .... So I did this tiny ad instead:
Two software engineers solve most of the world's problems in one K&R sized book
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic