how to remove MouseListener / ActionListener on a JTextField

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

 

时间: 2025-01-19 18:44:48

how to remove MouseListener / ActionListener on a JTextField的相关文章

java网络五子棋的源代码

网络|源代码 下面的源代码分为4个文件:chessClient.java:客户端主程序.chessInterface.java:客户端的界面.chessPad.java:棋盘的绘制.chessServer.java:服务器端.可同时容纳50个人同时在线下棋,聊天.没有加上详细注释,不过绝对可以运行,j2sdk1.4下通过. /**************************************************************************************

进制转换-为什么我的程序运行不了。。。求大神帮忙

问题描述 为什么我的程序运行不了...求大神帮忙 package 跑; import java.awt.*;import java.awt.event.*; import javax.swing.*;import javax.swing.event.*; public class JinZhi extends JFrame implements ActionListener{ JLabel jl1jl2jl3jl4;JTextField jt1jt2jt3jt4;public JinZhi(){

求解答-五子棋游戏,运行结果出错,但是我总是检查不出来,希望得到帮助!!!

问题描述 五子棋游戏,运行结果出错,但是我总是检查不出来,希望得到帮助!!! import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import javax.swing.*; import java.awt.*; public class Example2 exte

求解答-运行结果正确,在连续5黑或5白可以自动出现一个对话框,显示赢了也可选继续或退出游戏

问题描述 运行结果正确,在连续5黑或5白可以自动出现一个对话框,显示赢了也可选继续或退出游戏 import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import javax.swing.*; import java.awt.*; public class Examp

java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]用户

问题描述 java.sql.SQLException:[Microsoft][SQLServer2000DriverforJDBC][SQLServer]用户'sa'登录失败.这是连接数据库的代码:packagedatabase;importjava.sql.*;publicclassConnectionDB{publicstaticConnectiongetConnection(){Connectionconn=null;Stringurl="jdbc:sqlserver://localhos

java加密解密示例分享_java

(1)先定义要实现的类,我先定义了一个抽象类 复制代码 代码如下: //图形类  abstract class  Shape{       int x,y;       int x1,y1;      Color color ;      Graphics g ;      byte type ;      public abstract void draw(Graphics g) ;  }  //直线类   class LineShape extends Shape{      public

Java套接字(Socket)网络编程入门_java

网络应用模式主要有: 主机/终端模式:集中计算,集中管理: 客户机/服务器(Client/Server,简称C/S)模式:分布计算,分布管理: 浏览器/服务器模式:利用Internet跨平台. www(万维网)就是建立在客户机/服务器模式上,以HTML语言和HTTP协议为基础,能够提供各种Internet服务的信息浏览系统.网络信息放在主机的不同位置,www服务器利用超文本链路链接各项信息.www客户机(浏览器Brower)负责与服务器建立联系,向服务器发送请求,处理HTML超媒体,提供图形用户

java中UDP简单聊天程序实例代码_java

学过计算机网络通信的都知道,计算机之间传送数据由两种,即TCP通信和UDP通信.TCP是可靠的面向连接的通信协议,二UDP是不可靠的面向无连接的通信协议. java中有基于TCP的网络套接字通信,也有基于UDP的用户数据报通信,UDP的信息传输速度快,但不可靠! 基于UDP通信的基本模式: (1)将数据打包,称为数据包(好比将信件装入信封一样),然后将数据包发往目的地. (2)接受别人发来的数据包(好比接收信封一样),然后查看数据包中的内容. 客户机 复制代码 代码如下: package com

求解答-运行结果说是Exception in thread "main" java.NoClassDeFoundError:Example

问题描述 运行结果说是Exception in thread "main" java.NoClassDeFoundError:Example import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import javax.swing.*; import j