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. 关于《s3c2416裸跑环境配置》一文的一些补充

    <s3c2416裸跑环境配置>一文已经发表很长一段时间了,前两天突然收到邮件提示有人回复,原来网友jxyggg按照文中所讲去调试,却始终不能成功.问题的描述见原文后的回复,经过QQ交流,问 ...

  2. QT是否流行还是和历史有关啊(各个平台不同时间的方案都讲到了)

    这个还是和历史有关啊..现在基于Qt的桌面软件越来越多的...许多GTK的也在向Qt迁移..可以说在XP时代,微软自己有一套MFC,和成熟的vs系列开发工具..而Qt-Creator是09左右才有项目 ...

  3. 将QT开发的界面程序封装成DLL,在VC中成功调用

    最近手头的一个项目需要做一个QT界面,并且封装成DLL,然后再动态调用DLL给出的接口函数,使封装在DLL内部的QT界面跑起来,在网上查了很多资料,今天终于成功了,经验不敢独享,因为CSDN给了我很多 ...

  4. linux source命令学习

    1. linux source命令的作用? 我们可能经常需要修改到诸如/etc/profile,~/.bash_profile等这样的配置文件, 一方面我们希望所作的修改在当前的环境中立即生效: 另一 ...

  5. bzoj1684 [Usaco2005 Oct]Close Encounter

    Description Lacking even a fifth grade education, the cows are having trouble with a fraction proble ...

  6. svn小技巧——重定向svn diff

    svn diff的默认输出模式比较冗长,如果遇到修改比较多的情况,有时会较难看清diff.svn本身提供了自定义diff输出的选项,可能的修改方法如下: 建立一个脚本文件(如svndiff.sh),调 ...

  7. debian msyql 5.1 卸载与安装

    卸载:apt-get autoremove --purge mysql-server-5.1 卸载服务端 apt-get remove mysql-common #一定要卸载(包含配置文件) dpkg ...

  8. IOS将UIView转化为UIImage

    +(UIImage*)createImageFromView:(UIView*)view { //obtain scale CGFloat scale = [UIScreen mainScreen]. ...

  9. 利用iptables将本地的80端口请求转发到8080,当前主机ip为192.168.1.1,命令怎么写?

    iptables -t nat -A PREROUTING -d 192.168.1.1 -p tcp --dport 80 -j REDIRECT --to-port 8080 内网上外网: ipt ...

  10. Android服务端本地窗口FramebufferNativeWindow

    Android窗口系统 我们知道Android系统采用OpenGL来绘制3D图形,OpenGL ES提供了本地窗口(NativeWindow)的概念,无论是在Android平台中还是其他平台中,只要实 ...