简体   繁体   中英

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?

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
  }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM