how to remove MouseListener / ActionListener on a JTextField

简介: I have the following code adding an ActionListener to a JTextField: chatInput.addMouseListener(new java.

I have the following code adding an ActionListener to a JTextField:

chatInput.addMouseListener(new java.awt.event.MouseAdapter() {
    public void mouseClicked(java.awt.event.MouseEvent evt) {
       chatInputMouseClicked(evt);
    }
});
AI 代码解读

Now how do I remove this MouseListener using chatInput.removeMouseListener(), since this function needs an argument?

 

You can consider 3 approaches:

1) Save reference to your listener before adding it so you can remove it later:

MouseListener ml = new MouseAdapter() {
    public void mouseClicked(java.awt.event.MouseEvent evt) {
        chatInputMouseClicked(evt);
    }
};
chatInput.addMouseListener (ml);
...
chatInput.removeMouseListener (ml);
AI 代码解读

2) You can get all certain event listeners with correspondent methods like:

public MouseListener[] getMouseListeners()  
AI 代码解读

or

public EventListener[] getListeners(Class listenerType)
AI 代码解读

Here are the javadocs for the first and second methods. If you can identify among all listeners the one which you want to remove or if you want to remove all listeners this approach may help.


3) You can use some boolean variable which will 'turn off' your listener. But you should notice that the variable should be a field of outer class:

private boolean mouseListenerIsActive;

public void doSmthWithMouseListeners () {
    mouseListenerIsActive = true;

    chatInput.addMouseListener(new MouseAdapter() {
        public void mouseClicked(MouseEvent evt) {
            if (mouseListenerIsActive) {
               chatInputMouseClicked(evt);
            }
        }
    });
}

public void stopMouseListner () {
    mouseListenerIsActive = false;
}
AI 代码解读

I would prefer the third one because it gives some flexibility and if I want to turn on mouse listener again I will not need to create new object.

https://stackoverflow.com/questions/2627946/how-to-remove-mouselistener-actionlistener-on-a-jtextfield

 

 java.awt.event
Class ComponentEvent
java.lang.Object
  java.util.EventObject
      java.awt.AWTEvent
          java.awt.event.ComponentEvent

 

java.awt.event
Interface MouseListener
All Superinterfaces:
EventListener
All Known Subinterfaces:
MouseInputListener

 

    private void ProcessComponents(JPanel jPanel){
        JButton jButton=getButtonFromJPanel(jPanel);
        JTextField jTextFiled=getTextFieldFromJPanel(jPanel);
        jTextFiled.setText("");
        
        removeClickListener(jButton);
        removeClickListener(jTextFiled);
        jButton.setEnabled(false);
        jTextFiled.setEnabled(false);
    }
    
    
    private  JButton  getButtonFromJPanel(JPanel jPanel){
        if (jPanel!=null) {
            Component[] component= jPanel.getComponents();
            if (component!=null) {
                for (Component c : component) {
                    if (c instanceof JButton) {
                        return (JButton) c;
                    }
                }
            }
        }
        debugPrn.info("no button in the panel");
        return new JButton();
    }
    
    private  JTextField  getTextFieldFromJPanel(JPanel jPanel){
        if (jPanel!=null) {
            Component[] component= jPanel.getComponents();
            if (component!=null) {
                for (Component c : component) {
                    if (c instanceof JTextField) {
                        return (JTextField) c;
                    }
                }
            }
        }
        debugPrn.info("no textField in the panel");
        return new JTextField();
    }
    
    
    
    private void removeClickListener(JComponent jComponent){
        
        ComponentListener[] cls = jComponent.getComponentListeners();
        if (cls != null) {
            for (ComponentListener cl : cls) {
                jComponent.removeComponentListener(cl);
            }
        }

        MouseListener[] mls = jComponent.getMouseListeners();
        if (mls != null) {
            for (MouseListener ml : mls) {
                jComponent.removeMouseListener(ml);
            }
        }
    }

 

目录
打赏
0
0
0
0
95
分享
相关文章
Foreach循环中为什么不要进行remove/add操作
Foreach循环中为什么不要进行remove/add操作
150 0
List的remove操作一定要小心!
List的remove操作一定要小心!
Iterator remove()详解
一、Iterator的API      关于Iterator主要有三个方法:hasNext()、next()、remove()      hasNext:没有指针下移操作,只是判断是否存在下一个元素      next:指针下移,返回该指针所指向的元素     remove:删除当前指针所指向的元素,一般和next方法一起用,这时候的作用就是删除next方法返回的元素二、迭代器原理     
2586 0
Remove Element
Remove Element删掉指定的元素,并用后面的元素顶替空出来的位置;Remove ElementGiven an array and a value, remove all instances of that value in place and return the new length.
707 0
foreach中不允许对元素进行add和remove底层原理
foreach中不允许对元素进行add和remove底层原理 🍅 Java学习路线:搬砖工的Java学习路线 🍅 作者微信公众号:程序员小王 🍅 程序员小王的博客:https://www.wolai.com/wnaghengjie/ahNwvAUPG2Hb1Sy7Z8waaF 🍅 扫描主页左侧二维码,加我微信 一起学习、一起进步 🍅 欢迎点赞 👍 收藏 ⭐留言 📝
140 0
foreach中不允许对元素进行add和remove底层原理
FragmentTransation中的remove和detach有什么区别?
remove(): 从Activity中移除一个Fragment,如果被移除的Fragment没有添加到回退栈(回退栈后面会详细说),这个Fragment实例将会被销毁; detach(): 会将view从UI中移除,和remove()不同,此时fragment的状态依然由FragmentManager维护。
1251 0
List.remove 报错 UnsupportedOperationException
Java中List.remove(removeRange,clear类似) 报出 UnsupportedOperationException 的错误。原来该List是一个AbstractList,不支持增删改操作。
1267 0