• 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

Basic MouseListener question

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, really simple question, but bizarrely I can't seem to find any examples online...

Given:

JButton button1 = new JButton("One");
button1.addMouseListener(this);
JButton button2 = new JButton("Two");
button2.addMouseListener(this);


and then:

public void mousePressed(MouseEvent e) { }

How do I determine whether MouseEvent e was triggered by button1 or button2? I know how to do it with an ActionListener (giving it a name, and checking it), but I'm specifically interested in the mouse press as opposed to a mouse click for both buttons.

Many thanks in advance,
Chris
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if(e.getSource() == button1)
 
Chris Flynn
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Michael Dunn:
if(e.getSource() == button1)




Ah! Thankyou.
 
reply
    Bookmark Topic Watch Topic
  • New Topic