how to remove MouseListener / ActionListener on a JTextField

本文介绍了三种从JTextField上移除MouseListener的方法:保存监听器引用以便后续移除;获取所有MouseListener并移除指定的监听器;使用布尔变量来禁用监听器的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

 

转载于:https://www.cnblogs.com/softidea/p/4042321.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值