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. PASCAL的优越性:官方的说法(不需要Makefile,节约大量的时间)

    也许你认为为什么我选择pascal代替其他的语言,像C.或者您会拿FreePascal和其他的pascal编译器作比较,那么好,这里您看看FreePascal为什么好: 1.pascal是一个非常简洁 ...

  2. C# 委托2

    委托的定义: (1) 将方法作为变量使用的一种机制,就是将方法当作变量用(声明,赋值,传参)   (2) 将变量当作方法来用,首先就要去声明变量,就要考虑变量的类型,就是(委托变量,对应方法的返回值, ...

  3. C# 新特性_协变与逆变 (.net 4.0)

    C#4.0中有一个新特性:协变与逆变.可能很多人在开发过程中不常用到,但是深入的了解他们,肯定是有好处的. 协变和逆变体现在泛型的接口和委托上面,也就是对泛型参数的声明,可以声明为协变,或者逆变.什么 ...

  4. 【转】飞凌嵌入式(Forlinx)TE/OK6410内核编译:“make: arm-none-linux-gnueabi-gcc:命令未找到”

    原文网址:http://www.xuebuyuan.com/1104711.html Ubuntu10.04下编译飞凌嵌入式(Forlinx)TE/OK6410开发板提供的内核2.6.36 本以为按照 ...

  5. UESTC_我要长高 CDOJ 594

    韩父有N个儿子,分别是韩一,韩二…韩N.由于韩家演技功底深厚,加上他们间的密切配合,演出获得了巨大成功,票房甚至高达2000万.舟子是名很有威望的公知,可是他表面上两袖清风实则内心阴暗,看到韩家红红火 ...

  6. hdu 4501 小明系列故事——买年货_二维背包

    题目:你可以有v1元,v2代金券,v3个物品免单,现在有n个商品,商品能用纸币或者代金券购买,当然你可以买v3个商品免费.问怎么最大能买多少价值 题意: 思路二维背包,dp[v1][v2][v3]=M ...

  7. 用数据说话,外贸B2C产品选择(上篇)-热门搜索法

    当选择了外贸这条路,那就是选择了跟外国人做生意.那面对全球这么大的市场究竟选什么样的产品才干脱颖而出?什么样的产品才是全球卖家喜欢的呢?什么样的产品才干让自己財源滚滚?我想这都是全部刚開始外贸创业的人 ...

  8. RMAN数据库恢复之恢复表空间和数据文件

    执行表空间或数据文件恢复时,数据库既可以是MOUNT状态,也可以是OPEN状态.1.恢复表空间在执行恢复之前,如果被操作的表空间未处理OFFLINE状态,必须首先通过ALTER TABLESPACE… ...

  9. 【数学.前左上计数法】【HDU1220】Cube

    Cube Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

  10. HttpApplication事件执行顺序(转)

    HttpApplication 类的实例(Global继承自该类)是在 ASP.NET 基础结构中创建的,而不是由用户直接创建的.HttpApplication 类的一个实例在其生存期内被用于处理多个 ...