加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

e609. Listening to All Focus Changes Between Components in a

发布时间:2020-12-14 03:46:55 所属栏目:大数据 来源:网络整理
导读: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. KeyboardFocusManag

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读