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:ActionFocusMover.java

public void actionPerformed(ActionEvent actionEvent) {
    KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
    manager.focusNextComponent();
}

From source file:Main.java

public static Window getActiveWindow() {
    KeyboardFocusManager mgr = KeyboardFocusManager.getCurrentKeyboardFocusManager();
    return mgr.getActiveWindow();
}

From source file:Main.java

/**
 * Requests focus unless the component already has focus. For some weird
 * reason calling {@link Component#requestFocusInWindow()}when the
 * component is focus owner changes focus owner to another component!
 *
 * @param component the component to request focus for
 * @return true if the component has focus or probably will get focus,
 *         otherwise false//from   w w w  . j av  a  2s  .  c  o m
 */
public static boolean requestFocus(Component component) {
    /*
     * System.out.println("Owner: " +
     * System.identityHashCode(KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner()) + ", " +
     * System.identityHashCode(component) + ", " +
     * (KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner() ==
     * component));
     */
    return KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner() == component
            || component.requestFocusInWindow();
}

From source file:com.willwinder.universalgcodesender.uielements.components.CommandTextArea.java

public final void init(BackendAPI backend) {
    this.backend = backend;
    if (backend != null) {
        this.backend.addUGSEventListener(this);
        this.setEnabled(backend.isConnected());
    }//from  w w  w.j  av  a  2 s.  c o  m

    this.addActionListener((ActionEvent evt) -> action(evt));
    KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(this);
}

From source file:org.pentaho.reporting.designer.core.actions.global.ScreenCaptureAction.java

public static void installGlobally() {
    if (!installed) {
        installed = true;/* w  ww  . j  a  v  a2 s .c o  m*/
        KeyboardFocusManager.getCurrentKeyboardFocusManager()
                .addKeyEventPostProcessor(new GlobalKeyEventHandler());
    }
}

From source file:org.formic.wizard.form.gui.component.FileChooser.java

/**
 * Creates a new instance.//from ww w .j a v a2  s  .co m
 */
public FileChooser(int selectionMode, String choosable) {
    super(new BorderLayout());
    chooser = new JFileChooser() {
        private static final long serialVersionUID = 1L;

        // force "proper" behavior of <enter> when a button has focus
        protected boolean processKeyBinding(KeyStroke key, KeyEvent event, int condition, boolean pressed) {
            if (event.getKeyCode() == KeyEvent.VK_ENTER) {
                Component focusOwner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
                // if a button has focus, click it.
                if (focusOwner instanceof JButton) {
                    ((JButton) focusOwner).doClick();
                    return true;
                }
            }
            return super.processKeyBinding(key, event, condition, pressed);
        }
    };
    chooser.setFileSelectionMode(selectionMode);
    addChoosableFileFilters(choosable);

    textField = new JTextField();
    button = new JButton(Installer.getString("browse.text"));

    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            int result = chooser.showOpenDialog(getParent());
            if (result == JFileChooser.APPROVE_OPTION) {
                textField.setText(chooser.getSelectedFile().getPath());
                textField.requestFocus();
            }
        }
    });
    add(textField, BorderLayout.CENTER);
    add(button, BorderLayout.EAST);
}

From source file:org.pentaho.reporting.designer.core.actions.global.ScreenCaptureAction.java

public static void saveScreenShot(final int modifiers) {
    final Component component = KeyboardFocusManager.getCurrentKeyboardFocusManager().getActiveWindow();
    final GraphicsConfiguration graphicsConfiguration = component.getGraphicsConfiguration();
    final GraphicsDevice graphicsDevice = graphicsConfiguration.getDevice();
    try {/* w ww .  java  2  s  . c om*/
        final Robot robot = new Robot(graphicsDevice);
        final BufferedImage image;
        if ((modifiers & ActionEvent.SHIFT_MASK) == ActionEvent.SHIFT_MASK) {
            image = robot.createScreenCapture(graphicsConfiguration.getBounds());
        } else {
            image = robot.createScreenCapture(component.getBounds());
        }

        final String homeDirectory = ReportDesignerBoot.getInstance().getGlobalConfig()
                .getConfigProperty("user.home", ".");
        final File homeDir = new File(homeDirectory);
        final File f = generateName(homeDir);
        if (f == null) {
            return;
        }
        final FileOutputStream fout = new FileOutputStream(f);
        try {
            final PngEncoder encoder = new PngEncoder();
            encoder.setCompressionLevel(6);
            encoder.setEncodeAlpha(false);
            encoder.setImage(image);
            final byte[] bytes = encoder.pngEncode();
            fout.write(bytes);
        } finally {
            fout.close();
        }
    } catch (IOException ioe) {
        UncaughtExceptionsModel.getInstance().addException(ioe);
    } catch (AWTException e1) {
        // ignore
        UncaughtExceptionsModel.getInstance().addException(e1);
    }
}

From source file:com.jidesoft.spring.richclient.docking.JideApplicationPage.java

public JideApplicationPage(ApplicationWindow window, PageDescriptor pageDescriptor) {
    super(window, pageDescriptor);
    if (log.isInfoEnabled()) {
        log.info("Constructing Application page " + pageDescriptor.getId());
    }/*w  w  w . j a  va 2s . com*/
    this.window = (JideApplicationWindow) window;
    KeyboardFocusManager.getCurrentKeyboardFocusManager().addPropertyChangeListener("focusOwner",
            new FocusOwnerChangeListener());
}

From source file:misc.TrackFocusDemo.java

public TrackFocusDemo() {
    super(new BorderLayout());

    JPanel mugshots = new JPanel(new GridLayout(2, 3));
    pic1 = new Picture(createImageIcon("images/" + mayaString + ".jpg", mayaString).getImage());
    pic1.setName("1");
    mugshots.add(pic1);/*  w w w . j a v a2  s .  com*/
    pic2 = new Picture(createImageIcon("images/" + anyaString + ".jpg", anyaString).getImage());
    pic2.setName("2");
    mugshots.add(pic2);
    pic3 = new Picture(createImageIcon("images/" + laineString + ".jpg", laineString).getImage());
    pic3.setName("3");
    mugshots.add(pic3);
    pic4 = new Picture(createImageIcon("images/" + cosmoString + ".jpg", cosmoString).getImage());
    pic4.setName("4");
    mugshots.add(pic4);
    pic5 = new Picture(createImageIcon("images/" + adeleString + ".jpg", adeleString).getImage());
    pic5.setName("5");
    mugshots.add(pic5);
    pic6 = new Picture(createImageIcon("images/" + alexiString + ".jpg", alexiString).getImage());
    pic6.setName("6");
    mugshots.add(pic6);

    info = new JLabel("Nothing selected");

    setPreferredSize(new Dimension(450, 350));
    add(mugshots, BorderLayout.CENTER);
    add(info, BorderLayout.PAGE_END);
    setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

    KeyboardFocusManager focusManager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
    focusManager.addPropertyChangeListener(new PropertyChangeListener() {
        public void propertyChange(PropertyChangeEvent e) {
            String prop = e.getPropertyName();
            if (("focusOwner".equals(prop)) && ((e.getNewValue()) instanceof Picture)) {

                Component comp = (Component) e.getNewValue();
                String name = comp.getName();
                Integer num = new Integer(name);
                int index = num.intValue();
                if (index < 0 || index > comments.length) {
                    index = 0;
                }
                info.setText(comments[index]);
            }
        }
    });
}

From source file:TrackFocusDemo.java

public TrackFocusDemo() {
    super(new BorderLayout());

    JPanel mugshots = new JPanel(new GridLayout(2, 3));
    pic1 = new Picture(createImageIcon("images/" + mayaString + ".jpg", mayaString).getImage());
    pic1.setName("1");
    mugshots.add(pic1);/* ww w  .  java  2s.  c  om*/
    pic2 = new Picture(createImageIcon("images/" + anyaString + ".jpg", anyaString).getImage());
    pic2.setName("2");
    mugshots.add(pic2);
    pic3 = new Picture(createImageIcon("images/" + laineString + ".jpg", laineString).getImage());
    pic3.setName("3");
    mugshots.add(pic3);
    pic4 = new Picture(createImageIcon("images/" + cosmoString + ".jpg", cosmoString).getImage());
    pic4.setName("4");
    mugshots.add(pic4);
    pic5 = new Picture(createImageIcon("images/" + adeleString + ".jpg", adeleString).getImage());
    pic5.setName("5");
    mugshots.add(pic5);
    pic6 = new Picture(createImageIcon("images/" + alexiString + ".jpg", alexiString).getImage());
    pic6.setName("6");
    mugshots.add(pic6);

    info = new JLabel("Nothing selected");

    setPreferredSize(new Dimension(450, 350));
    add(mugshots, BorderLayout.CENTER);
    add(info, BorderLayout.PAGE_END);
    setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

    KeyboardFocusManager focusManager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
    focusManager.addPropertyChangeListener(new PropertyChangeListener() {
        public void propertyChange(PropertyChangeEvent e) {
            String prop = e.getPropertyName();
            if (("focusOwner".equals(prop)) && (e.getNewValue() != null)
                    && ((e.getNewValue()) instanceof Picture)) {

                Component comp = (Component) e.getNewValue();
                String name = comp.getName();
                Integer num = new Integer(name);
                int index = num.intValue();
                if (index < 0 || index > comments.length) {
                    index = 0;
                }
                info.setText(comments[index]);
            }
        }
    });
}