• Post Reply Bookmark Topic Watch Topic
  • New Topic

Basic MouseListener question  RSS feed

 
Chris Flynn
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • 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
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if(e.getSource() == button1)
 
Chris Flynn
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Michael Dunn:
if(e.getSource() == button1)



Ah! Thankyou.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!