How to remove mouselistener/actionlistener on a jtextfield ()

Source: Internet
Author: User

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 usingchatInput.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 shocould notice that the variable shocould 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 wocould 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);            }        }    }

 

How to remove mouselistener/actionlistener on a jtextfield ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.