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那么不必看关于 ...
随机推荐
- LeetCode_Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings. class Solutio ...
- undefined reference to `png_set_longjmp_fn'
这个是在Linux上编译项目的时候,一个动态库层用到的一个函数实现未找到,即使我链接了libpng2也没有找到,原因是这个库老了一些,没有这个函数定义,需要链接更高版本的png库,CentOS上有了在 ...
- 关于找不到stdafx.h头文件问题
代码: #include "stdafx.h" #include "stdlib.h" char* getcharBuffer() { return " ...
- nginx+vaadin配置
nginx+Vaadin的特殊性在于配置WEBSOCKET或LONG_POLLING.网上资料不多,自己多次尝试配置都不成功,后来终于找到这篇说明才得以配置成功,使用效果不错,介绍如下. 1./etc ...
- Triangle 解答
Question Given a triangle, find the minimum path sum from top to bottom. Each step you may move to a ...
- python刷取CSDN博文访问量之四
python刷取CSDN博文访问量之四 作者:vpoet #coding:utf-8 import requests import urllib2 import re import time def ...
- Libgdx开发ios游戏
今天亲自尝试了LibGDX如何开发ios游戏, 必须条件: 1:mac操作系统,mac下必须安装Xcode 好像ios开发必须在mac操作系统下 2:mac下安装eclipse 3:eclip ...
- ExtJS+ASP.NET自己定义曲线
第一步:创建Store数据源 var myData = []; myData.push({ 'name': '1', 'Oil_Production': '30', 'Water_Injection' ...
- 【C语言】输入一组整数,求出这组数字子序列和中最大值
//输入一组整数.求出这组数字子序列和中最大值 #include <stdio.h> int MAxSum(int arr[],int len) { int maxsum = 0; int ...
- Ubuntu 12.04 搭建 Eclipse Android 开发环境(转)
Ubuntu 12.04 搭建 Eclipse Android 开发环境 http://blog.sina.com.cn/s/blog_93dc666c0101b39p.html (2012-09-0 ...