tM --
tM --

Reputation: 207

How can I use an ActionListener to perform an action on mouse release/mouse up?

I have a JButton with an attached ActionListener. The action is performed when the button is clicked, but I want the action to be performed after the click (ie when the mouse button is released). How can I do this?

Upvotes: 0

Views: 2570

Answers (1)

Dan D.
Dan D.

Reputation: 32391

You cannot do this with an ActionListener. You will have to add a MouseListener and handle the mouseReleased event.

Example:

addMouseListener(new MouseListener() {
  public void mousePressed(MouseEvent e) {
  }

  public void mouseReleased(MouseEvent e) {
    // TODO: add your code here
  }

  public void mouseClicked(MouseEvent e) {
  }

  public void mouseEntered(MouseEvent e) {
  }

  public void mouseExited(MouseEvent e) {
  }
});

Or even easier, with a MouseAdapter:

addMouseListener(new MouseAdapter() {
  public void mouseReleased(MouseEvent e) {
    // TODO: add your code here
  }
});

Upvotes: 4

Related Questions