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.
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的更多相关文章
- java学习笔记(详细)
java平台 1.J2SE java开发平台标准版 2.J2EE java开发平台企业版 java程序需要在虚拟机上才可以运行,换言之只要有虚拟机的系统都可以运行java程序.不同系统上要安装对应的虚 ...
- swing Event-Listener-Adapter 对照表
Source Event Event Listener AbstractButton (JButton,JToggleButton, JCheckBox,JRadioButton ActionEven ...
- JAVA车票管理系统(简单GUI)
一. 需求分析 1.设计题目:车票管理系统 用JAVA语言和数据结构知识设计设计车票管理系统.要求如下所述: 一车站每天有n个发车班次,每个班次都有一个班次号(1.2.3…n),固定的发车时间, ...
- 04747_Java语言程序设计(一)_第6章_图形界面设计(二)
例6.1声明一个面板子类,面板子类对象有3个选择框. class Panel1 extends JPanel { JCheckBox box1, box2, box3; Panel1() { box1 ...
- java通讯录
)设一个通信录由以下几项数据信息构成: 数据项 类型 姓名 字符串 地址 字符串 邮政编码 ...
- java围棋游戏源代码
//李雨泽源代码,不可随意修改.//时间:2017年9月22号.//地点:北京周末约科技有限公司.//package com.bao; /*围棋*/ /*import java.awt.*; impo ...
- Java知多少(89)列表和组合框
有两种类型的菜单:下拉式菜单和弹出式菜单.本章只讨论下拉式菜单编程方法.菜单与JComboBox和JCheckBox不同,它们在界面中是一直可见的.菜单与JComboBox的相同之处是每次只可选择一个 ...
- Java知多少(90)菜单
有两种类型的菜单:下拉式菜单和弹出式菜单.本章只讨论下拉式菜单编程方法.菜单与JComboBox和JCheckBox不同,它们在界面中是一直可见的.菜单与JComboBox的相同之处是每次只可选择一个 ...
- Java学生管理系统(连接数据库查询)超详细
这几天逼着交Java,借鉴各位师傅的做出来这么个简陋的东西,各位大师傅不要笑我.(学都没有学过Java的我,QAQ~) 下面针对的都是SQL Server系列的连接,如果你使用MySQL那么不必看关于 ...
随机推荐
- USB系列之八:透过ASPI执行SCSI命令
在<USB系列之七>里我们介绍了ASPI的规范,并对一系列ASPI的命令做了测试,其中的02号命令是执行SCSI命令,我们专门在这篇文章中介绍,在<USB系列七>中,我们已经了 ...
- T-SQL 创建、修改、删除数据库,表语法
CREATE 语句 CREATE语句的开头都是一样的,然后是特定的细节. CREATE <object type> <object name> 一.CREATE DATABAS ...
- jQuery插件之Form
一.jQuery.Form.js 插件的作用是实现Ajax提交表单. 方法: 1.formSerilize() 用于序列化表单中的数据,并将其自动整理成适合AJAX异步请求的URL地址格式. 2.cl ...
- Asp.net 处理程序(第五篇)
HttpApplication有19个标准事件,当到达第8个事件PostMapRequestHandler触发的时候,标志着已经获取到了处理请求的处理程序对象,在第11个事件PreRequestHan ...
- sqlserver 创建索引
语法:CREATE [索引类型] INDEX 索引名称ON 表名(列名)WITH FILLFACTOR = 填充因子值0~100GO /*实例*/ CREATE NONCLUSTERED INDEX ...
- Linux 时间定时同步操作
Yum –y install ntp安装时钟同步服务加入开机启动Chkcongfig ntpd on添加自动校对时间,每十分钟校对一次Crontab –e */10 * * * * /usr/sbin ...
- hihoCoder #1234 : Fractal(数学简单题)
时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 This is the logo of PKUACM 2016. More specifically, the logo i ...
- IOS 计算密码强度
+ (BOOL) judgeRange:(NSArray*)conditionArr Password:(NSString*)password { NSRange range; BOOL result ...
- svn 清理失败 (cleanup 失败) 的解决方法
svn 清理失败 (clean up 失败) 的解决方法 參考:http://www.tuicool.com/articles/biy6na 解决方法: step1: 到 sqlite官网 (http ...
- [每日一题] OCP1z0-047 :2013-08-18 禁用启用约束――主键与外键 ..................................61
正确答案:C 根据题意,测试结果如下: 1.创建表emp,并且设emp_no字段为主键,设mgr_no字段为外键. gyj@MYDB> create table emp 2 (emp_no nu ...