Java Code Examples for java.awt.KeyboardFocusManager#getCurrentKeyboardFocusManager()

The following examples show how to use java.awt.KeyboardFocusManager#getCurrentKeyboardFocusManager() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ShortcutAndMenuKeyEventProcessor.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public static synchronized void uninstall() {
    if(!installed) {
        return;
    }
    
    ShortcutAndMenuKeyEventProcessor instance = getDefault();
    
    KeyboardFocusManager keyboardFocusManager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
    keyboardFocusManager.removeKeyEventDispatcher(instance);
    keyboardFocusManager.removeKeyEventPostProcessor(instance);
    // reset default focus traversal keys
    keyboardFocusManager.setDefaultFocusTraversalKeys(
            KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, defaultForward
    );                
    keyboardFocusManager.setDefaultFocusTraversalKeys(
            KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, defaultBackward
    );                
    defaultBackward = null;
    defaultForward = null;
}
 
Example 2
Source File: TestFocusFreeze.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) {
    boolean all_passed = true;
    KeyboardFocusManager testKFM = new TestKFM(robot);
    KeyboardFocusManager defKFM = KeyboardFocusManager.getCurrentKeyboardFocusManager();

    for (int i = 0; i < 10; i++) {
        test(testKFM, defKFM);
        Util.waitForIdle(robot);
        System.out.println("Iter " + i + ": " + (lock.get() ? "passed." : "failed!"));
        if (!lock.get()) {
            all_passed = false;
        }
    }
    if (!all_passed) {
        throw new RuntimeException("Test failed: not all iterations passed!");
    }
    System.out.println("Test passed.");
}
 
Example 3
Source File: TestFocusFreeze.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) {
    boolean all_passed = true;
    KeyboardFocusManager testKFM = new TestKFM(robot);
    KeyboardFocusManager defKFM = KeyboardFocusManager.getCurrentKeyboardFocusManager();

    for (int i = 0; i < 10; i++) {
        test(testKFM, defKFM);
        Util.waitForIdle(robot);
        System.out.println("Iter " + i + ": " + (lock.get() ? "passed." : "failed!"));
        if (!lock.get()) {
            all_passed = false;
        }
    }
    if (!all_passed) {
        throw new RuntimeException("Test failed: not all iterations passed!");
    }
    System.out.println("Test passed.");
}
 
Example 4
Source File: BasicPopupMenuUI.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
private void doReturn() {
    KeyboardFocusManager fmgr =
        KeyboardFocusManager.getCurrentKeyboardFocusManager();
    Component focusOwner = fmgr.getFocusOwner();
    if(focusOwner != null && !(focusOwner instanceof JRootPane)) {
        return;
    }

    MenuSelectionManager msm = MenuSelectionManager.defaultManager();
    MenuElement path[] = msm.getSelectedPath();
    MenuElement lastElement;
    if(path.length > 0) {
        lastElement = path[path.length-1];
        if(lastElement instanceof JMenu) {
            MenuElement newPath[] = new MenuElement[path.length+1];
            System.arraycopy(path,0,newPath,0,path.length);
            newPath[path.length] = ((JMenu)lastElement).getPopupMenu();
            msm.setSelectedPath(newPath);
        } else if(lastElement instanceof JMenuItem) {
            JMenuItem mi = (JMenuItem)lastElement;

            if (mi.getUI() instanceof BasicMenuItemUI) {
                ((BasicMenuItemUI)mi.getUI()).doClick(msm);
            }
            else {
                msm.clearSelectedPath();
                mi.doClick(0);
            }
        }
    }
}
 
Example 5
Source File: BasicPopupMenuUI.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
private void doReturn() {
    KeyboardFocusManager fmgr =
        KeyboardFocusManager.getCurrentKeyboardFocusManager();
    Component focusOwner = fmgr.getFocusOwner();
    if(focusOwner != null && !(focusOwner instanceof JRootPane)) {
        return;
    }

    MenuSelectionManager msm = MenuSelectionManager.defaultManager();
    MenuElement path[] = msm.getSelectedPath();
    MenuElement lastElement;
    if(path.length > 0) {
        lastElement = path[path.length-1];
        if(lastElement instanceof JMenu) {
            MenuElement newPath[] = new MenuElement[path.length+1];
            System.arraycopy(path,0,newPath,0,path.length);
            newPath[path.length] = ((JMenu)lastElement).getPopupMenu();
            msm.setSelectedPath(newPath);
        } else if(lastElement instanceof JMenuItem) {
            JMenuItem mi = (JMenuItem)lastElement;

            if (mi.getUI() instanceof BasicMenuItemUI) {
                ((BasicMenuItemUI)mi.getUI()).doClick(msm);
            }
            else {
                msm.clearSelectedPath();
                mi.doClick(0);
            }
        }
    }
}
 
Example 6
Source File: BasicPopupMenuUI.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
private void doReturn() {
    KeyboardFocusManager fmgr =
        KeyboardFocusManager.getCurrentKeyboardFocusManager();
    Component focusOwner = fmgr.getFocusOwner();
    if(focusOwner != null && !(focusOwner instanceof JRootPane)) {
        return;
    }

    MenuSelectionManager msm = MenuSelectionManager.defaultManager();
    MenuElement path[] = msm.getSelectedPath();
    MenuElement lastElement;
    if(path.length > 0) {
        lastElement = path[path.length-1];
        if(lastElement instanceof JMenu) {
            MenuElement newPath[] = new MenuElement[path.length+1];
            System.arraycopy(path,0,newPath,0,path.length);
            newPath[path.length] = ((JMenu)lastElement).getPopupMenu();
            msm.setSelectedPath(newPath);
        } else if(lastElement instanceof JMenuItem) {
            JMenuItem mi = (JMenuItem)lastElement;

            if (mi.getUI() instanceof BasicMenuItemUI) {
                ((BasicMenuItemUI)mi.getUI()).doClick(msm);
            }
            else {
                msm.clearSelectedPath();
                mi.doClick(0);
            }
        }
    }
}
 
Example 7
Source File: ContainerFocusAutoTransferTest.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
public void init() {
    robot = Util.createRobot();
    kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager();
    Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
        public void eventDispatched(AWTEvent event) {
            System.out.println("--> " + event);
        }
    }, FocusEvent.FOCUS_EVENT_MASK | WindowEvent.WINDOW_FOCUS_EVENT_MASK);
}
 
Example 8
Source File: BasicPopupMenuUI.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private void doReturn() {
    KeyboardFocusManager fmgr =
        KeyboardFocusManager.getCurrentKeyboardFocusManager();
    Component focusOwner = fmgr.getFocusOwner();
    if(focusOwner != null && !(focusOwner instanceof JRootPane)) {
        return;
    }

    MenuSelectionManager msm = MenuSelectionManager.defaultManager();
    MenuElement path[] = msm.getSelectedPath();
    MenuElement lastElement;
    if(path.length > 0) {
        lastElement = path[path.length-1];
        if(lastElement instanceof JMenu) {
            MenuElement newPath[] = new MenuElement[path.length+1];
            System.arraycopy(path,0,newPath,0,path.length);
            newPath[path.length] = ((JMenu)lastElement).getPopupMenu();
            msm.setSelectedPath(newPath);
        } else if(lastElement instanceof JMenuItem) {
            JMenuItem mi = (JMenuItem)lastElement;

            if (mi.getUI() instanceof BasicMenuItemUI) {
                ((BasicMenuItemUI)mi.getUI()).doClick(msm);
            }
            else {
                msm.clearSelectedPath();
                mi.doClick(0);
            }
        }
    }
}
 
Example 9
Source File: Commitable.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
static void sendCommitToFocusOwner() {
    KeyboardFocusManager focusManager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
    Component            focus        = focusManager.getPermanentFocusOwner();
    if (focus == null) {
        focus = focusManager.getFocusOwner();
    }
    if (focus instanceof Commitable) {
        ((Commitable) focus).attemptCommit();
    }
}
 
Example 10
Source File: BasicPopupMenuUI.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
private void doReturn() {
    KeyboardFocusManager fmgr =
        KeyboardFocusManager.getCurrentKeyboardFocusManager();
    Component focusOwner = fmgr.getFocusOwner();
    if(focusOwner != null && !(focusOwner instanceof JRootPane)) {
        return;
    }

    MenuSelectionManager msm = MenuSelectionManager.defaultManager();
    MenuElement path[] = msm.getSelectedPath();
    MenuElement lastElement;
    if(path.length > 0) {
        lastElement = path[path.length-1];
        if(lastElement instanceof JMenu) {
            MenuElement newPath[] = new MenuElement[path.length+1];
            System.arraycopy(path,0,newPath,0,path.length);
            newPath[path.length] = ((JMenu)lastElement).getPopupMenu();
            msm.setSelectedPath(newPath);
        } else if(lastElement instanceof JMenuItem) {
            JMenuItem mi = (JMenuItem)lastElement;

            if (mi.getUI() instanceof BasicMenuItemUI) {
                ((BasicMenuItemUI)mi.getUI()).doClick(msm);
            }
            else {
                msm.clearSelectedPath();
                mi.doClick(0);
            }
        }
    }
}
 
Example 11
Source File: ContainerFocusAutoTransferTest.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
public void init() {
    robot = Util.createRobot();
    kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager();
    Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
        public void eventDispatched(AWTEvent event) {
            System.out.println("--> " + event);
        }
    }, FocusEvent.FOCUS_EVENT_MASK | WindowEvent.WINDOW_FOCUS_EVENT_MASK);
}
 
Example 12
Source File: DrawingPanel.java    From osp with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Method mouseMoved
 *
 * @param e
 */
public void mouseMoved(MouseEvent e) {
  KeyboardFocusManager focuser =
  		KeyboardFocusManager.getCurrentKeyboardFocusManager();
  Component focusOwner = focuser.getFocusOwner();
  if (focusOwner != null && !(focusOwner instanceof JTextComponent)) {
  	requestFocusInWindow();
  }
}
 
Example 13
Source File: ComponentProvider.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the context object which corresponds to the
 * area of focus within this provider's component.  Null
 * is returned when there is no context.
 * @param event popup event which corresponds to this request.
 * May be null for key-stroke or other non-mouse event.
 */
@Override
public ActionContext getActionContext(MouseEvent event) {
	Component c = getComponent();
	KeyboardFocusManager kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager();
	Component focusedComponent = kfm.getFocusOwner();
	if (focusedComponent != null && SwingUtilities.isDescendingFrom(focusedComponent, c)) {
		c = focusedComponent;
	}
	return createContext(c, null);
}
 
Example 14
Source File: RequestFocusAndHideTest.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws InterruptedException, java.lang.reflect.InvocationTargetException
{
    final Frame frame = new Frame("the test");
    frame.setLayout(new FlowLayout());
    final Button btn1 = new Button("button 1");
    frame.add(btn1);
    frame.add(new Button("button 2"));
    frame.add(new Button("button 3"));
    frame.pack();
    frame.setVisible(true);

    Robot r = Util.createRobot();
    Util.waitForIdle(r);
    Util.clickOnComp(btn1, r);
    Util.waitForIdle(r);
    KeyboardFocusManager kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager();
    if (kfm.getFocusOwner() != btn1) {
        throw new RuntimeException("test error: can not set focus on " + btn1 + ".");
    }

    EventQueue.invokeAndWait(new Runnable() {
            public void run() {
                final int n_comps = frame.getComponentCount();
                for (int i = 0; i < n_comps; ++i) {
                    frame.getComponent(i).setVisible(false);
                }
            }
        });
    Util.waitForIdle(r);
    final Component focus_owner = kfm.getFocusOwner();

    if (focus_owner != null && !focus_owner.isVisible()) {
        throw new RuntimeException("we have invisible focus owner");
    }
    System.out.println("test passed");
    frame.dispose();
}
 
Example 15
Source File: BasicPopupMenuUI.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private void doReturn() {
    KeyboardFocusManager fmgr =
        KeyboardFocusManager.getCurrentKeyboardFocusManager();
    Component focusOwner = fmgr.getFocusOwner();
    if(focusOwner != null && !(focusOwner instanceof JRootPane)) {
        return;
    }

    MenuSelectionManager msm = MenuSelectionManager.defaultManager();
    MenuElement path[] = msm.getSelectedPath();
    MenuElement lastElement;
    if(path.length > 0) {
        lastElement = path[path.length-1];
        if(lastElement instanceof JMenu) {
            MenuElement newPath[] = new MenuElement[path.length+1];
            System.arraycopy(path,0,newPath,0,path.length);
            newPath[path.length] = ((JMenu)lastElement).getPopupMenu();
            msm.setSelectedPath(newPath);
        } else if(lastElement instanceof JMenuItem) {
            JMenuItem mi = (JMenuItem)lastElement;

            if (mi.getUI() instanceof BasicMenuItemUI) {
                ((BasicMenuItemUI)mi.getUI()).doClick(msm);
            }
            else {
                msm.clearSelectedPath();
                mi.doClick(0);
            }
        }
    }
}
 
Example 16
Source File: RequestFocusAndHideTest.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws InterruptedException, java.lang.reflect.InvocationTargetException
{
    final Frame frame = new Frame("the test");
    frame.setLayout(new FlowLayout());
    final Button btn1 = new Button("button 1");
    frame.add(btn1);
    frame.add(new Button("button 2"));
    frame.add(new Button("button 3"));
    frame.pack();
    frame.setVisible(true);

    Robot r = Util.createRobot();
    Util.waitForIdle(r);
    Util.clickOnComp(btn1, r);
    Util.waitForIdle(r);
    KeyboardFocusManager kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager();
    if (kfm.getFocusOwner() != btn1) {
        throw new RuntimeException("test error: can not set focus on " + btn1 + ".");
    }

    EventQueue.invokeAndWait(new Runnable() {
            public void run() {
                final int n_comps = frame.getComponentCount();
                for (int i = 0; i < n_comps; ++i) {
                    frame.getComponent(i).setVisible(false);
                }
            }
        });
    Util.waitForIdle(r);
    final Component focus_owner = kfm.getFocusOwner();

    if (focus_owner != null && !focus_owner.isVisible()) {
        throw new RuntimeException("we have invisible focus owner");
    }
    System.out.println("test passed");
    frame.dispose();
}
 
Example 17
Source File: RequestFocusAndHideTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws InterruptedException, java.lang.reflect.InvocationTargetException
{
    final Frame frame = new Frame("the test");
    frame.setLayout(new FlowLayout());
    final Button btn1 = new Button("button 1");
    frame.add(btn1);
    frame.add(new Button("button 2"));
    frame.add(new Button("button 3"));
    frame.pack();
    frame.setVisible(true);

    Robot r = Util.createRobot();
    Util.waitForIdle(r);
    Util.clickOnComp(btn1, r);
    Util.waitForIdle(r);
    KeyboardFocusManager kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager();
    if (kfm.getFocusOwner() != btn1) {
        throw new RuntimeException("test error: can not set focus on " + btn1 + ".");
    }

    EventQueue.invokeAndWait(new Runnable() {
            public void run() {
                final int n_comps = frame.getComponentCount();
                for (int i = 0; i < n_comps; ++i) {
                    frame.getComponent(i).setVisible(false);
                }
            }
        });
    Util.waitForIdle(r);
    final Component focus_owner = kfm.getFocusOwner();

    if (focus_owner != null && !focus_owner.isVisible()) {
        throw new RuntimeException("we have invisible focus owner");
    }
    System.out.println("test passed");
    frame.dispose();
}
 
Example 18
Source File: BasicPopupMenuUI.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
private void doReturn() {
    KeyboardFocusManager fmgr =
        KeyboardFocusManager.getCurrentKeyboardFocusManager();
    Component focusOwner = fmgr.getFocusOwner();
    if(focusOwner != null && !(focusOwner instanceof JRootPane)) {
        return;
    }

    MenuSelectionManager msm = MenuSelectionManager.defaultManager();
    MenuElement path[] = msm.getSelectedPath();
    MenuElement lastElement;
    if(path.length > 0) {
        lastElement = path[path.length-1];
        if(lastElement instanceof JMenu) {
            MenuElement newPath[] = new MenuElement[path.length+1];
            System.arraycopy(path,0,newPath,0,path.length);
            newPath[path.length] = ((JMenu)lastElement).getPopupMenu();
            msm.setSelectedPath(newPath);
        } else if(lastElement instanceof JMenuItem) {
            JMenuItem mi = (JMenuItem)lastElement;

            if (mi.getUI() instanceof BasicMenuItemUI) {
                ((BasicMenuItemUI)mi.getUI()).doClick(msm);
            }
            else {
                msm.clearSelectedPath();
                mi.doClick(0);
            }
        }
    }
}
 
Example 19
Source File: MainFrameMenu.java    From jpexs-decompiler with GNU General Public License v3.0 4 votes vote down vote up
private void registerHotKeys() {

        KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
        manager.addKeyEventDispatcher(keyEventDispatcher = this::dispatchKeyEvent);
    }
 
Example 20
Source File: EditingPopUp.java    From niftyeditor with Apache License 2.0 4 votes vote down vote up
public TransferActionListener() {
    KeyboardFocusManager manager = KeyboardFocusManager.
       getCurrentKeyboardFocusManager();
    manager.addPropertyChangeListener("permanentFocusOwner", this);
}