Removing MouseListener() from a JLabel

烈酒焚心 提交于 2019-12-11 23:36:43

问题


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?


回答1:


In your mouse listener:

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



回答2:


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



来源:https://stackoverflow.com/questions/13363865/removing-mouselistener-from-a-jlabel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!