📄 keyboardfocusmanager.java
字号:
Component permanentFocusOwner = getGlobalPermanentFocusOwner (); setGlobalFocusOwner (null); setGlobalPermanentFocusOwner (null); // Inform the old focus owner that it has lost permanent // focus. if (focusOwner != null) { // We can't cache the event queue, because of // bootstrapping issues. We need to set the default // KeyboardFocusManager in EventQueue before the event // queue is started. EventQueue q = Toolkit.getDefaultToolkit ().getSystemEventQueue (); if (focusOwner != permanentFocusOwner) q.postEvent (new FocusEvent (focusOwner, FocusEvent.FOCUS_LOST, true)); else q.postEvent (new FocusEvent (focusOwner, FocusEvent.FOCUS_LOST, false)); } if (focusOwner != permanentFocusOwner) { EventQueue q = Toolkit.getDefaultToolkit ().getSystemEventQueue (); q.postEvent (new FocusEvent (permanentFocusOwner, FocusEvent.FOCUS_LOST, false)); } } } /** * Retrieve the {@link Component} that has the permanent keyboard * focus, or null if the focus owner was not set by a thread in the * current {@link java.lang.ThreadGroup}. * * @return the keyboard focus owner or null */ public Component getPermanentFocusOwner () { return (Component) getObject (currentPermanentFocusOwners); } /** * Retrieve the {@link Component} that has the permanent keyboard * focus, regardless of whether or not it was set by a thread in the * current {@link java.lang.ThreadGroup}. * * @return the keyboard focus owner * @throws SecurityException if this is not the keyboard focus * manager associated with the current {@link java.lang.ThreadGroup} */ protected Component getGlobalPermanentFocusOwner () { return (Component) getGlobalObject (currentPermanentFocusOwners); } /** * Set the {@link Component} that will be returned by {@link * #getPermanentFocusOwner} (when it is called from the current * {@link java.lang.ThreadGroup}) and {@link * #getGlobalPermanentFocusOwner}. This method does not actually * transfer the keyboard focus. * * @param focusOwner the Component to return from * getPermanentFocusOwner and getGlobalPermanentFocusOwner * * @see Component#requestFocus() * @see Component#requestFocusInWindow() */ protected void setGlobalPermanentFocusOwner (Component focusOwner) { if (focusOwner == null || focusOwner.focusable) setGlobalObject (currentPermanentFocusOwners, focusOwner, "permanentFocusOwner"); } /** * Retrieve the {@link Window} that is or contains the keyboard * focus owner, or null if the focused window was not set by a * thread in the current {@link java.lang.ThreadGroup}. * * @return the focused window or null */ public Window getFocusedWindow () { return (Window) getObject (currentFocusedWindows); } /** * Retrieve the {@link Window} that is or contains the focus owner, * regardless of whether or not the {@link Window} was set focused * by a thread in the current {@link java.lang.ThreadGroup}. * * @return the focused window * @throws SecurityException if this is not the keyboard focus * manager associated with the current {@link java.lang.ThreadGroup} */ protected Window getGlobalFocusedWindow () { return (Window) getGlobalObject (currentFocusedWindows); } /** * Set the {@link Window} that will be returned by {@link * #getFocusedWindow} (when it is called from the current {@link * java.lang.ThreadGroup}) and {@link #getGlobalFocusedWindow}. * This method does not actually cause <code>window</code> to become * the focused {@link Window}. * * @param window the Window to return from getFocusedWindow and * getGlobalFocusedWindow */ protected void setGlobalFocusedWindow (Window window) { if (window == null || window.focusable) setGlobalObject (currentFocusedWindows, window, "focusedWindow"); } /** * Retrieve the active {@link Window}, or null if the active window * was not set by a thread in the current {@link * java.lang.ThreadGroup}. * * @return the active window or null */ public Window getActiveWindow() { return (Window) getObject (currentActiveWindows); } /** * Retrieve the active {@link Window}, regardless of whether or not * the {@link Window} was made active by a thread in the current * {@link java.lang.ThreadGroup}. * * @return the active window * @throws SecurityException if this is not the keyboard focus * manager associated with the current {@link java.lang.ThreadGroup} */ protected Window getGlobalActiveWindow() { return (Window) getGlobalObject (currentActiveWindows); } /** * Set the {@link Window} that will be returned by {@link * #getActiveWindow} (when it is called from the current {@link * java.lang.ThreadGroup}) and {@link #getGlobalActiveWindow}. This * method does not actually cause <code>window</code> to be made * active. * * @param window the Window to return from getActiveWindow and * getGlobalActiveWindow */ protected void setGlobalActiveWindow(Window window) { setGlobalObject (currentActiveWindows, window, "activeWindow"); } /** * Retrieve the default {@link FocusTraversalPolicy}. * Focus-managing {@link Container}s use the returned object to * define their initial focus traversal policy. * * @return a non-null default FocusTraversalPolicy object */ public FocusTraversalPolicy getDefaultFocusTraversalPolicy () { if (defaultPolicy == null) defaultPolicy = new DefaultFocusTraversalPolicy (); return defaultPolicy; } /** * Set the {@link FocusTraversalPolicy} returned by {@link * #getDefaultFocusTraversalPolicy}. Focus-managing {@link * Container}s created after this call will use policy as their * initial focus traversal policy. Existing {@link Container}s' * focus traversal policies will not be affected by calls to this * method. * * @param policy the FocusTraversalPolicy that will be returned by * subsequent calls to getDefaultFocusTraversalPolicy * @throws IllegalArgumentException if policy is null */ public void setDefaultFocusTraversalPolicy (FocusTraversalPolicy policy) { if (policy == null) throw new IllegalArgumentException (); firePropertyChange ("defaultFocusTraversalPolicy", defaultPolicy, policy); defaultPolicy = policy; } /** * Set the default {@link java.util.Set} of focus traversal keys for * one of the focus traversal directions. * * @param id focus traversal direction identifier * @param keystrokes set of AWTKeyStrokes * * @see #FORWARD_TRAVERSAL_KEYS * @see #BACKWARD_TRAVERSAL_KEYS * @see #UP_CYCLE_TRAVERSAL_KEYS * @see #DOWN_CYCLE_TRAVERSAL_KEYS */ public void setDefaultFocusTraversalKeys (int id, Set keystrokes) { if (id != KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS && id != KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS && id != KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS && id != KeyboardFocusManager.DOWN_CYCLE_TRAVERSAL_KEYS) throw new IllegalArgumentException (); if (keystrokes == null) throw new IllegalArgumentException (); Set sa; Set sb; Set sc; String type; switch (id) { case FORWARD_TRAVERSAL_KEYS: sa = defaultFocusKeys[BACKWARD_TRAVERSAL_KEYS]; sb = defaultFocusKeys[UP_CYCLE_TRAVERSAL_KEYS]; sc = defaultFocusKeys[DOWN_CYCLE_TRAVERSAL_KEYS]; type = "forwardDefaultFocusTraversalKeys"; break; case BACKWARD_TRAVERSAL_KEYS: sa = defaultFocusKeys[FORWARD_TRAVERSAL_KEYS]; sb = defaultFocusKeys[UP_CYCLE_TRAVERSAL_KEYS]; sc = defaultFocusKeys[DOWN_CYCLE_TRAVERSAL_KEYS]; type = "backwardDefaultFocusTraversalKeys"; break; case UP_CYCLE_TRAVERSAL_KEYS: sa = defaultFocusKeys[FORWARD_TRAVERSAL_KEYS]; sb = defaultFocusKeys[BACKWARD_TRAVERSAL_KEYS]; sc = defaultFocusKeys[DOWN_CYCLE_TRAVERSAL_KEYS]; type = "upCycleDefaultFocusTraversalKeys"; break; case DOWN_CYCLE_TRAVERSAL_KEYS: sa = defaultFocusKeys[FORWARD_TRAVERSAL_KEYS]; sb = defaultFocusKeys[BACKWARD_TRAVERSAL_KEYS]; sc = defaultFocusKeys[UP_CYCLE_TRAVERSAL_KEYS]; type = "downCycleDefaultFocusTraversalKeys"; break; default: throw new IllegalArgumentException (); } int i = keystrokes.size (); Iterator iter = keystrokes.iterator (); while (--i >= 0) { Object o = iter.next (); if (!(o instanceof AWTKeyStroke) || sa.contains (o) || sb.contains (o) || sc.contains (o) || ((AWTKeyStroke) o).keyCode == KeyEvent.VK_UNDEFINED) throw new IllegalArgumentException (); } keystrokes = Collections.unmodifiableSet (new HashSet (keystrokes)); firePropertyChange (type, defaultFocusKeys[id], keystrokes); defaultFocusKeys[id] = keystrokes; } /** * Retrieve the default {@link java.util.Set} of focus traversal * keys for one of the focus traversal directions. * * @param id focus traversal direction identifier * * @return the default set of AWTKeyStrokes * * @see #FORWARD_TRAVERSAL_KEYS * @see #BACKWARD_TRAVERSAL_KEYS * @see #UP_CYCLE_TRAVERSAL_KEYS * @see #DOWN_CYCLE_TRAVERSAL_KEYS */ public Set getDefaultFocusTraversalKeys (int id) { if (id < FORWARD_TRAVERSAL_KEYS || id > DOWN_CYCLE_TRAVERSAL_KEYS) throw new IllegalArgumentException (); return defaultFocusKeys[id]; } /** * Retrieve the current focus cycle root, or null if the focus owner * was not set by a thread in the current {@link * java.lang.ThreadGroup}. * * @return the current focus cycle root or null */ public Container getCurrentFocusCycleRoot () { return (Container) getObject (currentFocusCycleRoots); } /** * Retrieve the current focus cycle root, regardless of whether or * not it was made set by a thread in the current {@link * java.lang.ThreadGroup}. * * @return the current focus cycle root * @throws SecurityException if this is not the keyboard focus * manager associated with the current {@link java.lang.ThreadGroup} */ protected Container getGlobalCurrentFocusCycleRoot () { return (Container) getGlobalObject (currentFocusCycleRoots); } /** * Set the {@link Container} that will be returned by {@link * #getCurrentFocusCycleRoot} (when it is called from the current * {@link java.lang.ThreadGroup}) and {@link * #getGlobalCurrentFocusCycleRoot}. This method does not actually * make <code>cycleRoot</code> the current focus cycle root. * * @param cycleRoot the focus cycle root to return from * getCurrentFocusCycleRoot and getGlobalCurrentFocusCycleRoot */ public void setGlobalCurrentFocusCycleRoot (Container cycleRoot) { setGlobalObject (currentFocusCycleRoots, cycleRoot, "currentFocusCycleRoot"); } /** * Registers the supplied property change listener for receiving * events caused by the following property changes: * * <ul> * <li>the current focus owner ("focusOwner")</li> * <li>the permanent focus owner ("permanentFocusOwner")</li> * <li>the focused window ("focusedWindow")</li> * <li>the active window ("activeWindow")</li> * <li>the default focus traversal policy ("defaultFocusTraversalPolicy")</li> * <li>the default set of forward traversal keys ("forwardDefaultFocusTraversalKeys")</li> * <li>the default set of backward traversal keys ("backwardDefaultFocusTraversalKeys")</li> * <li>the default set of up cycle traversal keys ("upCycleDefaultFocusTraversalKeys")</li> * <li>the default set of down cycle traversal keys ("downCycleDefaultFocusTraversalKeys")</li> * <li>the current focus cycle root ("currentFocusCycleRoot")</li> * </ul> * * If the supplied listener is null, nothing occurs. * * @param l the new listener to register. * @see KeyboardFocusManager#addPropertyChangeListener(String, java.beans.PropertyChangeListener) */ public void addPropertyChangeListener(PropertyChangeListener l) { if (l != null) propertyChangeSupport.addPropertyChangeListener(l); } /** * Removes the supplied property change listener from the list * of registered listeners. If the supplied listener is null, * nothing occurs. * * @param l the listener to remove. */ public void removePropertyChangeListener(PropertyChangeListener l) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -