码迷,mamicode.com
首页 > 移动开发 > 详细

e609. Listening to All Focus Changes Between Components in an Application

时间:2018-09-06 10:52:18      阅读:275      评论:0      收藏:0      [点我收藏+]

标签:one   sage   public   nts   pre   win   lse   ddp   sed   

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

e609. Listening to All Focus Changes Between Components in an Application

标签:one   sage   public   nts   pre   win   lse   ddp   sed   

原文地址:https://www.cnblogs.com/borter/p/9596088.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!