简体   繁体   中英

Removing MouseListener() from a JLabel

I added a MouseListener to a JLabel . Now if I want to disable this MouseListener associated with the JLabel , when the label is clicked once, how can I do it.

I know there is a big way to set a boolean or int variable when the label is clicked and then call a method and remove MouseListener there, but I want to learn a compact and easy way. Is there a way to do this?

In your mouse listener:

public void mouseClicked(MouseEvent event) {
    // Do stuff...
    ((Component) event.getSource()).removeMouseListener(this);
}

What's wrong with label.removeMouseListener(listener) ? It works just fine. If you want to create listener that removes itself call label.removeMouseListener(this)

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