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

Posted borter

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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

以上是关于e609. Listening to All Focus Changes Between Components in an Application的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces 543E. Listening to Music

How to find out which process is listening upon a port

The road to learning English-Listening

Open Yale course:Listening to Music

WARNING: The notebook server is listening on all IP addresses and not using encryption--Jupyter解决方案(

WARNING: The notebook server is listening on all IP addresses and not using encryption--Jupyter解决方案(