• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Identifying GUI events

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I have two buttons in a GUI, how do I distinguish the two button-click events? If I have

button1 = new JButton("Button 1");
button1.addActionListener(what_goes_here1?)
button2 = new JButton("Button 2");
button1.addActionListener(what_goes_here2?)

do I need two of the following, one for each button? If so, how do I distinguish the two events?

public void actionPerformed(ActionEvent e)

Thanks.
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Paul, first register ActionListener to your buttons like this:


and then in your actionperformed method write this:


I hope this helps you



kind regards
Igor

[ February 20, 2005: Message edited by: Igor Stojanovic ]
[ February 20, 2005: Message edited by: Igor Stojanovic ]
 
Paul Trow
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Igor,

Thanks - that was very helpful.

Paul
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Unless the actions performed by the two buttons are similar, I typically create two different event handlers. I also don't like to come up with names for them, so I use anonymouse inner classes. While the syntax is a little complicated the first time you see it, it doesn't take long for it to becomes second nature:


HTH

Layne

p.s. For future reference, we have a GUI forum where such question are more appropriate. You should probably mosey over there if you have more questions along these lines.
[ February 22, 2005: Message edited by: Layne Lund ]
 
Igor Stojanovic
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Layne Lund:
I also don't like to come up with names for them, so I use anonymouse inner classes.



Thx for the TIP Layne. Anonymouse classes are much better option when dealing with events.
[ February 22, 2005: Message edited by: Igor Stojanovic ]
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I like a style where the event handlers are extremely small ... one line to call another method. If you get into the Model View Controller pattern that method might be on a controller object. Imagine the handlers for the File/Exit menu item and an Exit button. Each has one line that calls the same exitTheApplication() method.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Stan James:
I like a style where the event handlers are extremely small ... one line to call another method. If you get into the Model View Controller pattern that method might be on a controller object. Imagine the handlers for the File/Exit menu item and an Exit button. Each has one line that calls the same exitTheApplication() method.



I think this is one reason why Swing introduced the Action class. Actions provide a nice mechanism for sharing code between menus, buttons, and other GUI controls. In fact, the API is such that you only add a line of code to register an action with the control upon creation. You don't need any extra code (beside what is in the Action class itself) to handle the event.

Layne
[ February 24, 2005: Message edited by: Layne Lund ]
 
Curse your sudden but inevitable betrayal! And this tiny ad too!
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic