Example usage for java.awt KeyboardFocusManager getCurrentKeyboardFocusManager

List of usage examples for java.awt KeyboardFocusManager getCurrentKeyboardFocusManager

Introduction

In this page you can find the example usage for java.awt KeyboardFocusManager getCurrentKeyboardFocusManager.

Prototype

public static KeyboardFocusManager getCurrentKeyboardFocusManager() 

Source Link

Document

Returns the current KeyboardFocusManager instance for the calling thread's context.

Usage

From source file:Main.java

public static void main(String[] args) {
    int ROWS = 100;
    JPanel content = new JPanel();
    content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));
    content.add(new JLabel("Thanks for helping out. Use tab to move around."));
    for (int i = 0; i < ROWS; i++) {
        JTextField field = new JTextField("" + i);
        field.setName("field#" + i);
        content.add(field);// ww  w .  j ava2  s .c  o m
    }
    KeyboardFocusManager.getCurrentKeyboardFocusManager().addPropertyChangeListener("focusOwner",
            new PropertyChangeListener() {
                @Override
                public void propertyChange(PropertyChangeEvent evt) {
                    if (!(evt.getNewValue() instanceof JComponent)) {
                        return;
                    }
                    JViewport viewport = (JViewport) content.getParent();
                    JComponent focused = (JComponent) evt.getNewValue();
                    if (content.isAncestorOf(focused)) {
                        Rectangle rect = focused.getBounds();
                        Rectangle r2 = viewport.getVisibleRect();
                        content.scrollRectToVisible(
                                new Rectangle(rect.x, rect.y, (int) r2.getWidth(), (int) r2.getHeight()));
                    }
                }
            });
    JFrame window = new JFrame();
    window.setContentPane(new JScrollPane(content));
    window.setSize(200, 200);
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setVisible(true);
}

From source file:Main.java

public static void main() {

    Window windowFocusOwner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusedWindow();

}

From source file:Main.java

public static void main() {

    Component compFocusOwner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();

}

From source file:Main.java

public static Component getNearestFocusableComponent(Component c, Container desiredRoot) {
    if (c == null)
        c = desiredRoot;/*  w w  w  .j  a v  a2  s  .  co  m*/
    if (c == null)
        c = KeyboardFocusManager.getCurrentKeyboardFocusManager().getActiveWindow();

    boolean cachedFocusCycleRoot = false;
    // make the desiredRoot into a focusCycleRoot
    if (desiredRoot != null) {
        cachedFocusCycleRoot = desiredRoot.isFocusCycleRoot();
        if (!cachedFocusCycleRoot)
            desiredRoot.setFocusCycleRoot(true);
    }

    Container focusRoot = null;
    if (c instanceof Container) {
        Container cnt = (Container) c;
        focusRoot = cnt.isFocusCycleRoot(cnt) ? cnt : cnt.getFocusCycleRootAncestor();
    } else
        focusRoot = c.getFocusCycleRootAncestor();

    Component focuser = null;
    if (focusRoot != null)
        //zw, remarked - selected component should become focused
        //focuser = focusRoot.getFocusTraversalPolicy().getLastComponent(focusRoot);
        focuser = c; //zw, added - selected component should become focused

    // restore the desiredRoot to its previous state
    if (desiredRoot != null && !cachedFocusCycleRoot) {
        desiredRoot.setFocusCycleRoot(cachedFocusCycleRoot);
    }
    return focuser;
}

From source file:Main.java

public static Component findPrevFocus() {
    Component c = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
    Container root = c.getFocusCycleRootAncestor();

    FocusTraversalPolicy policy = root.getFocusTraversalPolicy();
    Component prevFocus = policy.getComponentBefore(root, c);
    if (prevFocus == null) {
        prevFocus = policy.getDefaultComponent(root);
    }//from   w w w .  j  ava2s  . com
    return prevFocus;
}

From source file:Main.java

public static Component findNextFocus() {
    Component c = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
    Container root = c.getFocusCycleRootAncestor();

    FocusTraversalPolicy policy = root.getFocusTraversalPolicy();
    Component nextFocus = policy.getComponentAfter(root, c);
    if (nextFocus == null) {
        nextFocus = policy.getDefaultComponent(root);
    }/*from  w w  w . ja  v  a  2 s .com*/
    return nextFocus;
}

From source file:Main.java

public static final Component getFocusOwner() {
    return KeyboardFocusManager.getCurrentKeyboardFocusManager().getPermanentFocusOwner();
}

From source file:Main.java

public static Window getOwnerForChildWindow() {
    Window w = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusedWindow();
    if (w != null) {
        return w;
    }// w w  w.  java 2 s  .com
    w = KeyboardFocusManager.getCurrentKeyboardFocusManager().getActiveWindow();
    if (w != null) {
        return w;
    }
    /*
     * Priority level1
     * modal dialog: +200
     * non-modal dialog: +100
     * frame: +0
     *
     * Priority level2
     * no owned windows: +10
     */
    TreeMap<Integer, Window> prioMap = new TreeMap<Integer, Window>();
    for (Window cand : Window.getWindows()) {
        if (cand == null) {
            continue;
        }
        if (!cand.isVisible()) {
            continue;
        }
        if (!cand.isShowing()) {
            continue;
        }
        int prio = 0;
        Window[] children = cand.getOwnedWindows();
        if (children == null || children.length == 0) {
            prio += 10;
        }
        if (cand instanceof Dialog) {
            Dialog dlg = (Dialog) cand;
            if (dlg.isModal()) {
                prio += 200;
            } else {
                prio += 100;
            }
            prioMap.put(prio, cand);
        } else if (cand instanceof Frame) {
            if (!prioMap.containsKey(prio)) {
                prioMap.put(prio, cand);
            }
        }
    }
    if (prioMap.size() > 0) {
        return prioMap.get(prioMap.lastKey());
    }
    //last line of defense
    if (prioMap.size() == 0) {
        for (Window cand : Window.getWindows()) {
            if (cand == null) {
                continue;
            }
            if (cand.isVisible()) {
                return cand;
            }
        }
    }
    return null;
}

From source file:Main.java

/**
 * Adds the focus forward key.//from   w w w.  j  av a  2  s.c  o m
 *
 * @param button
 *            the button
 */
public static void addFocusForwardKey(final int button) {
    final KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
    final Set<?> forwardKeys = manager
            .getDefaultFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS);
    final Set<KeyStroke> newForwardKeys = new HashSet(forwardKeys);
    newForwardKeys.add(KeyStroke.getKeyStroke(button, 0));
    manager.setDefaultFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, newForwardKeys);
}

From source file:Main.java

/**
 * Tries to find the current focused window.
 * //from  w w  w .j av a 2  s .c om
 * @return the current focused window, or <code>null</code> if no such window
 *         could be found.
 */
public static final Window getCurrentWindow() {
    Window owner;
    final KeyboardFocusManager kbdFocusManager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
    owner = kbdFocusManager.getFocusedWindow();
    if (owner == null) {
        owner = kbdFocusManager.getActiveWindow();
    }
    return owner;
}