• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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 ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic