• 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
  • 相关阅读:
    [Web Security] Create a hash salt password which can stored in DB
    [Angular] Stagger animation v4.3.3
    [D3] Make D3 v4 Charts Responsive with the viewBox attribute
    [D3] Create Chart Axes with D3 v4
    [D3] Margin Convention with D3 v4
    [D3] Better Code Organization with selection.call() with D3 v4
    [D3] Basic Interactivity with D3 v4
    [Angular] Reactive Store and AngularFire Observables
    Google Play和基于Feature的过滤
    Three Swaps DFS
  • 原文地址:https://www.cnblogs.com/borter/p/9596088.html
Copyright © 2020-2023  润新知