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

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

  1. java学习笔记(详细)

    java平台 1.J2SE java开发平台标准版 2.J2EE java开发平台企业版 java程序需要在虚拟机上才可以运行,换言之只要有虚拟机的系统都可以运行java程序.不同系统上要安装对应的虚 ...

  2. swing Event-Listener-Adapter 对照表

    Source Event Event Listener AbstractButton (JButton,JToggleButton, JCheckBox,JRadioButton ActionEven ...

  3. JAVA车票管理系统(简单GUI)

    一.    需求分析 1.设计题目:车票管理系统 用JAVA语言和数据结构知识设计设计车票管理系统.要求如下所述: 一车站每天有n个发车班次,每个班次都有一个班次号(1.2.3…n),固定的发车时间, ...

  4. 04747_Java语言程序设计(一)_第6章_图形界面设计(二)

    例6.1声明一个面板子类,面板子类对象有3个选择框. class Panel1 extends JPanel { JCheckBox box1, box2, box3; Panel1() { box1 ...

  5. java通讯录

    )设一个通信录由以下几项数据信息构成: 数据项               类型 姓名                  字符串 地址                  字符串 邮政编码        ...

  6. java围棋游戏源代码

    //李雨泽源代码,不可随意修改.//时间:2017年9月22号.//地点:北京周末约科技有限公司.//package com.bao; /*围棋*/ /*import java.awt.*; impo ...

  7. Java知多少(89)列表和组合框

    有两种类型的菜单:下拉式菜单和弹出式菜单.本章只讨论下拉式菜单编程方法.菜单与JComboBox和JCheckBox不同,它们在界面中是一直可见的.菜单与JComboBox的相同之处是每次只可选择一个 ...

  8. Java知多少(90)菜单

    有两种类型的菜单:下拉式菜单和弹出式菜单.本章只讨论下拉式菜单编程方法.菜单与JComboBox和JCheckBox不同,它们在界面中是一直可见的.菜单与JComboBox的相同之处是每次只可选择一个 ...

  9. Java学生管理系统(连接数据库查询)超详细

    这几天逼着交Java,借鉴各位师傅的做出来这么个简陋的东西,各位大师傅不要笑我.(学都没有学过Java的我,QAQ~) 下面针对的都是SQL Server系列的连接,如果你使用MySQL那么不必看关于 ...

随机推荐

  1. C#设置标记方法等为否决的不可用

       C#如何标记类里面的方法或者类为否决的,不可使用.在VS IDE编辑器中使用此方法或者类时会用绿色的波浪线标记这个语句,当移动鼠标到这句代码上时,会出现[否决的]方法名,警告“方法名称”已过时: ...

  2. ./configure : /bin/sh^M : bad interpreter

    用命令行来编译Qt的时候发生标题尚的错误. 原因是文件中带有DOS行结束符,必须把它转换成UNix结束符 references: http://stackoverflow.com/questions/ ...

  3. spring3.0事务的配置

    第一种配置方法:基于XML的事务管理 这种方法不需要对原有的业务做任何修改,通过在XML文件中定义需要拦截方法的匹配即可完成配置,要求是,业务处理中的方法的命名要有规律,比如setXxx,xxxUpd ...

  4. django项目环境搭建备忘

    由于使用python3,所以尽量为每个项目配置虚拟环境来管理各个项目的=. 新建一个项目文件夹,进入该路径 python3 -m venv ll_env 然后激活虚拟环境 source ll_env/ ...

  5. JNI的替代者—使用JNA访问Java外部功能接口

    摘自:http://www.cnblogs.com/lanxuezaipiao/p/3635556.html JNI的替代者-使用JNA访问Java外部功能接口 1. JNA简单介绍 先说JNI(Ja ...

  6. iOS openURL方法实现打电话、发短信、发邮件、打开其他App

    UIApplication有个功能十分强大的openURL:方法 - (BOOL)openURL:(NSURL*)url; 通过这个方法,我们可以实现: 先获取 UIApplication UIApp ...

  7. cocos2dx lua 学习笔记(二)

    安装开发环境 sublime - http://www.sublimetext.com/2 package control - http://packagecontrol.io/installatio ...

  8. android笔试题集2

    1.请谈一下Android系统的架构.答:Android系统采用了分层架构,从高层到低层分别是应用程序层.应用程序框架层.系统运行库层和linux核心层. 2.谈谈android大众常用的五种布局.答 ...

  9. os x 10.10 測试版系统下载 swift语言学习资料下载

    http://pan.baidu.com/s/1eQ5oj1S               这是下载地址 ! 刚学完oc 就出了 swift!这----  只是还是非常高兴看了一点swith得东西感觉 ...

  10. react-native 环境配置及hello world

    一.前言 最近手头的工作繁多,有研究性的项目和系统研发,正好遇到同事离职,接手了框架的UI组件,不仅需要维护和填坑,还需要开发新的功能组件.因为身在H5-Hybird的框架部门,最近团队开始尝试使用R ...