zoukankan      html  css  js  c++  java
  • e609. Listening to All Focus Changes Between Components in an Application

    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
  • 相关阅读:
    【转载】Select2插件 IE下 autofocus bug的解决方法
    修改iphone自带的按钮样式
    使用swiper制作微场景遇到的问题
    使用Excel的VLOOKUP函数合并不同Sheet页两个表格中的相同项
    【转】判断浏览器版本是否是ie8以下浏览器,如果是调到提示换更高级版本浏览器页面
    跨域上传文件
    WEB前端学习中遇到的概念
    Rails Gem notification_exception
    Html5 drag and drop
    KeyCode
  • 原文地址:https://www.cnblogs.com/borter/p/9596088.html
Copyright © 2011-2022 走看看