e609. Listening to All Focus Changes Between Components in an Application
2024-10-10 21:28:31
To listen to focus change events between components, install a listener with the keyboard focus manager. If you need the ability to veto (reject) a focus change, install a vetoable listener with the keyboard focus manager.
KeyboardFocusManager.getCurrentKeyboardFocusManager()
.addPropertyChangeListener(new FocusChangeListener());
KeyboardFocusManager.getCurrentKeyboardFocusManager()
.addVetoableChangeListener(new FocusVetoableChangeListener()); class FocusChangeListener implements PropertyChangeListener {
public void propertyChange(PropertyChangeEvent evt) {
Component oldComp = (Component)evt.getOldValue();
Component newComp = (Component)evt.getNewValue(); if ("focusOwner".equals(evt.getPropertyName())) {
if (oldComp == null) {
// the newComp component gained the focus
} else {
// the oldComp component lost the focus
}
} else if ("focusedWindow".equals(evt.getPropertyName())) {
if (oldComp == null) {
// the newComp window gained the focus
} else {
// the oldComp window lost the focus
}
}
}
} class FocusVetoableChangeListener implements VetoableChangeListener {
public void vetoableChange(PropertyChangeEvent evt) throws PropertyVetoException {
Component oldComp = (Component)evt.getOldValue();
Component newComp = (Component)evt.getNewValue(); if ("focusOwner".equals(evt.getPropertyName())) {
if (oldComp == null) {
// the newComp component will gain the focus
} else {
// the oldComp component will lose the focus
}
} else if ("focusedWindow".equals(evt.getPropertyName())) {
if (oldComp == null) {
// the newComp window will gain the focus
} else {
// the oldComp window will lose the focus
}
} boolean vetoFocusChange = false;
if (vetoFocusChange) {
throw new PropertyVetoException("message", evt);
}
}
Related Examples |
最新文章
- JS函数运行在它们被定义的作用域内,而不是它们被执行的作用域内
- eclipse+spket+Extjs4.2.1开发环境搭建
- ORA-03113解决方法
- 151008:javascript不明白的地方
- Java魔法堂:自定义和解析注解
- 转 Citrix XenCenter安装VM之挂载ISO详解
- [GeekBand]C++高级编程技术(2)
- CF Covered Path (贪心)
- BestCoder Round #49
- 腾讯云上PhantomJS用法示例
- 【Docker】 积累
- 论C++的智能指针
- 利用box-shadow属性实现页面层叠效果
- git提交忽略某些文件或文件夹
- javascript 跨域 的几种方法
- git 线上回滚问题纪要
- 乘风破浪:LeetCode真题_026_Remove Duplicates from Sorted Array
- 0005python中的静态方法和类方法
- redis通过dump.db文件 进行数据替换 复制
- raspberry pi 树莓派作为比特比矿机