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
  • 相关阅读:
    public,protected,private辨析
    转载---Java集合对象的深度复制与普通复制
    SSM框架学习之高并发秒杀业务--笔记4-- web层
    PCB布线总的原则
    模拟电子技术目录
    放大器(PA+LAN)在射频上的应用
    AD软件Bug和自我失误的对战
    二、cadence焊盘与封装制作操作步骤详细说明
    图腾柱电路工作原理
    转载:介绍AD另外一种奇葩的多通道复用的方法
  • 原文地址:https://www.cnblogs.com/borter/p/9596088.html
Copyright © 2011-2022 走看看