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

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

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

public MouseListener[] getMouseListeners()  

or

public EventListener[] getListeners(Class listenerType)

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

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

 

相关文章
避免list中remove导致ConcurrentModificationException
避免list中remove导致ConcurrentModificationException
55 0
|
算法 C++ 容器
C++ std::remove/std::remove_if/erase用法探讨
std::remove 不会改变输入vector/string的长度。其过程相当于去除指定的字符,剩余字符往前靠。后面的和原始字符保持一致。
179 0
List的remove操作一定要小心!
List的remove操作一定要小心!
|
存储 Java 程序员
foreach中不允许对元素进行add和remove底层原理
foreach中不允许对元素进行add和remove底层原理 🍅 Java学习路线:搬砖工的Java学习路线 🍅 作者微信公众号:程序员小王 🍅 程序员小王的博客:https://www.wolai.com/wnaghengjie/ahNwvAUPG2Hb1Sy7Z8waaF 🍅 扫描主页左侧二维码,加我微信 一起学习、一起进步 🍅 欢迎点赞 👍 收藏 ⭐留言 📝
133 0
foreach中不允许对元素进行add和remove底层原理
ArrayList的remove()方法解读
remove()方法:要注意在remove()方法有两种形式: ①:remove(int index):按照集合下表查找,这个也是默认的,返回值是Object型。 ②:remove(Object o):按照value值进行移除操作涉及装箱,所以默认是第一种,其返回值是boolean型,表示操作是否成功。 remove(int index): @Test public void test1() { ArrayList arrayList...
219 0
|
物联网 Linux 开发者
Remove 函数|学习笔记
快速学习 Remove 函数
|
Java
Foreach循环中为什么不要进行remove/add操作
Foreach循环中为什么不要进行remove/add操作
123 0
list.remove(index)返回flase,移除失败
list.remove(index)返回flase,移除失败
123 0
list.remove(index)返回flase,移除失败
List.remove 报错 UnsupportedOperationException
Java中List.remove(removeRange,clear类似) 报出 UnsupportedOperationException 的错误。原来该List是一个AbstractList,不支持增删改操作。
1252 0
|
算法 C#
算法题丨Remove Element
描述 Given an array and a value, remove all instances of that value in-place and return the new length.
1048 0