Example usage for javax.swing JComponent requestFocus

List of usage examples for javax.swing JComponent requestFocus

Introduction

In this page you can find the example usage for javax.swing JComponent requestFocus.

Prototype

public void requestFocus() 

Source Link

Document

Requests that this Component gets the input focus.

Usage

From source file:Main.java

/**
 * Request focus on the given component if it doesn't already have it
 * and <code>isRequestFocusEnabled()</code> returns true.
 *//*from w w w.j  av  a2 s .  co m*/
public static void adjustFocus(JComponent c) {
    if (!c.hasFocus() && c.isRequestFocusEnabled()) {
        c.requestFocus();
    }
}

From source file:Main.java

public static void requestFocus(final JComponent component) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            component.requestFocus();
        }/*from  w ww. j  av  a  2s  .  c  om*/
    });
}

From source file:Main.java

public static void requestFocusOnDisplay(JComponent c) {
    final JComponent c2 = c;

    c2.addAncestorListener(new javax.swing.event.AncestorListener() {
        public void ancestorAdded(javax.swing.event.AncestorEvent event) {
            c2.requestFocus();
            c2.removeAncestorListener(this);
        }//from   ww  w. ja v  a  2 s  . co  m

        public void ancestorRemoved(javax.swing.event.AncestorEvent event) {
        }

        public void ancestorMoved(javax.swing.event.AncestorEvent event) {
        }
    });
}

From source file:Main.java

/**
 * Sets the hot key for focus./*from w  ww.java2 s . c  o m*/
 *
 * @param comp
 *            the comp
 * @param keyStroke
 *            the key stroke
 * @param actionName
 *            the action name
 */
public static void setHotKeyForFocus(final JComponent comp, final String keyStroke, final String actionName) {
    // get the button's Action map
    final ActionMap amap = comp.getActionMap();
    // add an action to the button's action map
    // and give it a name(it can be any object not just String)
    amap.put(actionName, new AbstractAction() {
        /**
         *
         */
        private static final long serialVersionUID = 1L;

        @Override
        public void actionPerformed(final ActionEvent e) {
            // call your a method that contains your action code
            comp.requestFocus();
        }
    });
    // get the input map for the button
    final InputMap imap = comp.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    // add a key stroke associated with an action in the action map(action
    // name).
    // imap.put(KeyStroke.getKeyStroke("F1"),"ActionName");
    // you can do the same for more than one key.
    imap.put(KeyStroke.getKeyStroke(keyStroke), actionName);
}

From source file:com.aw.swing.mvp.focus.ConcurrentFocusManager.java

public void invokeAndWait(final JComponent component) {
    final Runnable requestFocusRunner = new Runnable() {
        public void run() {
            logger.debug("Calling request focus in " + Thread.currentThread());
            component.requestFocus();
        }//  w  w w. j a  va 2 s.c om
    };

    Thread requestFocusThread = new Thread() {
        public void run() {
            try {
                SwingUtilities.invokeAndWait(requestFocusRunner);
            } catch (Exception e) {
                e.printStackTrace();
            }
            logger.debug("Finished on invoke and wait in " + Thread.currentThread());
        }
    };
    requestFocusThread.start();

}

From source file:com.aw.swing.mvp.focus.ConcurrentFocusManager.java

public void invokeLaterRequestFocus(final String description, final JComponent component) {
    if (component == null) {
        return;/*from  w ww.  ja  va  2s .com*/
    }
    invokeLater(description, new Runnable() {
        public void run() {
            logger.debug("Requesting focus in :" + component);
            component.requestFocus();
        }
    });
}

From source file:org.openmicroscopy.shoola.agents.metadata.editor.PropertiesUI.java

/**
 * Modifies the passed components depending on the value of the
 * passed flag./*from  w ww.j  av  a 2  s .  c  om*/
 * 
 * @param panel     The panel to handle.
 * @param field      The field to handle.
 * @param button   The button to handle.
 * @param editable   Pass <code>true</code> if  to <code>edit</code>,
 *                <code>false</code> otherwise.
 */
private void editField(JPanel panel, JComponent field, JButton button, boolean editable) {
    if (field == namePane) {
        //namePane.setEnabled(editable);
        editableName = editable;
        namePane.setEditable(editable);
        if (editable) {
            panel.setBorder(EDIT_BORDER_BLACK);
            field.requestFocus();
        } else {
            panel.setBorder(defaultBorder);
        }
        namePane.getDocument().removeDocumentListener(this);
        String text = namePane.getText();
        if (text != null)
            text = text.trim();
        if (editable)
            namePane.setText(modifiedName);
        else
            namePane.setText(UIUtilities.formatPartialName(text));
        namePane.getDocument().addDocumentListener(this);
        namePane.select(0, 0);
        namePane.setCaretPosition(0);
    } else if (field == descriptionWiki) {
        descriptionWiki.setEnabled(editable); //was editable
        if (editable) {
            descriptionScrollPane.setBorder(EDIT_BORDER_BLACK);
            field.requestFocus();
        } else {
            descriptionScrollPane.setBorder(EDIT_BORDER);
        }
    }
}