📄 customkeyboardfocusmanager.java
字号:
/* * CustomKeyboardFocusManager.java * * Copyright (C) 2002, 2003, 2004, 2005, 2006 Takis Diakoumis * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * */package org.underworldlabs.swing;import java.awt.Component;import java.awt.Container;import java.awt.DefaultKeyboardFocusManager;import java.awt.FocusTraversalPolicy;import java.beans.PropertyChangeEvent;import java.beans.PropertyChangeListener;import java.beans.PropertyVetoException;import java.beans.VetoableChangeListener;import javax.swing.JComponent;import javax.swing.JTextField;/* ---------------------------------------------------------- * CVS NOTE: Changes to the CVS repository prior to the * release of version 3.0.0beta1 has meant a * resetting of CVS revision numbers. * ---------------------------------------------------------- *//** <p>Custom KeyboardFocusManager to mainly control the focus of * <code>JTextField<code> objects within a container. This * provides the functionaliaty to select (highlight) any * text already contained within the field when focus is * gained through keyboard traversal (ie. TAB key). * * @author Takis Diakoumis * @version $Revision: 1.4 $ * @date $Date: 2006/05/14 06:56:07 $ */public class CustomKeyboardFocusManager extends DefaultKeyboardFocusManager { /** <p>Focuses the Component before aComponent, typically based on a * FocusTraversalPolicy. * * @param aComponent the Component that is the basis for the focus * traversal operation */ public void focusPreviousComponent(Component aComponent) { if (aComponent == null) return; if (aComponent.getParent() instanceof TextFieldFocusController) { Container rootAncestor = aComponent.getFocusCycleRootAncestor(); Component comp = aComponent; if (comp instanceof JTextField) { ((JTextField)comp).select(0, 0); } while (rootAncestor != null && !(rootAncestor.isShowing() && rootAncestor.isFocusable() && rootAncestor.isEnabled()) ) { comp = rootAncestor; rootAncestor = comp.getFocusCycleRootAncestor(); } if (rootAncestor != null) { FocusTraversalPolicy policy = rootAncestor.getFocusTraversalPolicy(); Component _component = policy.getComponentBefore(rootAncestor, comp); if (_component == null) _component = policy.getDefaultComponent(rootAncestor); if (_component != null) { if (_component instanceof JTextField) { ((JTextField)_component).selectAll(); } ((JComponent)_component).grabFocus(); } else { aComponent.transferFocusBackward(); } } } else { aComponent.transferFocusBackward(); } } /** <p>Focuses the Component after aComponent, typically based on a * FocusTraversalPolicy. * * @param the Component that is the basis for the focus * traversal operation */ public void focusNextComponent(Component aComponent) { if (aComponent == null) return; if (aComponent.getParent() instanceof TextFieldFocusController) { Container rootAncestor = aComponent.getFocusCycleRootAncestor(); Component comp = aComponent; if (comp instanceof JTextField) { ((JTextField)comp).select(0, 0); } while (rootAncestor != null && !(rootAncestor.isShowing() && rootAncestor.isFocusable() && rootAncestor.isEnabled()) ) { comp = rootAncestor; rootAncestor = comp.getFocusCycleRootAncestor(); } if (rootAncestor != null) { FocusTraversalPolicy policy = rootAncestor.getFocusTraversalPolicy(); Component _component = policy.getComponentAfter(rootAncestor, comp); if (_component == null) _component = policy.getDefaultComponent(rootAncestor); if (_component != null) { if (_component instanceof JTextField) { ((JTextField)_component).selectAll(); } ((JComponent)_component).grabFocus(); } else { aComponent.transferFocus(); } } } else { aComponent.transferFocus(); } } // --- some fiddling with the focus bits --- /* public CustomKeyboardFocusManager() { super();// addVetoableChangeListener(new FocusVetoableChangeListener()); addPropertyChangeListener(new FocusChangeListener()); } class FocusChangeListener implements PropertyChangeListener { public void propertyChange(PropertyChangeEvent evt) { Component oldComp = null; Component newComp = null; Object oldObject = evt.getOldValue(); Object newObject = evt.getNewValue(); if (oldObject != null && oldObject instanceof Component) { oldComp = (Component)oldObject; } if (newObject != null && newObject instanceof Component) { newComp = (Component)evt.getNewValue(); } if ("focusOwner".equals(evt.getPropertyName())) { if (oldComp != null) { System.out.println("oldComp: " + oldComp.getClass().getName()); } else { System.out.println("oldComp is null"); } if (newComp != null) { System.out.println("newComp: " + newComp.getClass().getName()); } else { System.out.println("newComp is null"); } } } } class FocusVetoableChangeListener implements VetoableChangeListener { public void vetoableChange(PropertyChangeEvent evt) throws PropertyVetoException { Component oldComp = (Component)evt.getOldValue(); Component newComp = (Component)evt.getNewValue(); boolean vetoFocusChange = false; if ("focusOwner".equals(evt.getPropertyName())) { if (newComp == null) { vetoFocusChange = true; // the newComp component will gain the focus } } if (vetoFocusChange) { throw new PropertyVetoException("message", evt); } } } */}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -