Example usage for javax.swing JComponent requestFocusInWindow

List of usage examples for javax.swing JComponent requestFocusInWindow

Introduction

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

Prototype

public boolean requestFocusInWindow() 

Source Link

Document

Requests that this Component gets the input focus.

Usage

From source file:Main.java

public static void focusOnOpen(final JComponent component) {
    /*/*from  ww  w  .j  ava 2s  . c o  m*/
    final Window window = SwingUtilities.getWindowAncestor(component);
    if (window != null)
      window.addWindowListener(new WindowAdapter() {
    public void windowOpened(WindowEvent e) {
      component.requestFocus();
      window.removeWindowListener(this);
    }
      });
     */

    component.addAncestorListener(new AncestorListener() {
        public void ancestorAdded(AncestorEvent event) {
            component.requestFocusInWindow();
            component.removeAncestorListener(this);
        }

        public void ancestorRemoved(AncestorEvent event) {
        }

        public void ancestorMoved(AncestorEvent event) {
        }
    });
}

From source file:hr.fer.zemris.vhdllab.platform.remoting.LoginForm.java

public boolean requestFocusInWindow() {
    // Put the focus on the right field
    String username = loginDetails.getUsername();
    JComponent field = (username != null && username.length() > 0) ? passwordField : usernameField;
    return field.requestFocusInWindow();
}

From source file:com.t3.macro.api.functions.input.ColumnPanel.java

/** Sets the focus to the control that last had it. */
public void restoreFocus() {
    //      // debugging
    //      String s = (onShowFocus instanceof JTextField) ?
    //         " (" + ((JTextField)onShowFocus).getText() + ")" : "";
    //      String c = (onShowFocus == null) ? "null" : onShowFocus.getClass().getName();
    //      System.out.println("  Shown: onShowFocus is " + c + s);

    if (onShowFocus != null) {
        onShowFocus.requestFocusInWindow();
    } else {/*  w ww  .  j  ava 2 s .  com*/
        JComponent first = findFirstFocusable();
        if (first != null)
            first.requestFocusInWindow();
    }
}

From source file:king.flow.action.DefaultMsgSendAction.java

protected void showDoneMsg(final TLSResult result) {
    SwingUtilities.invokeLater(new Runnable() {
        @Override//from ww w  . ja  v a 2  s.co  m
        public void run() {
            if (doneDisplayList.size() == 1) {
                showOnComponent(getBlockMeta(doneDisplayList.get(0)), result);
            } else {
                try {
                    JSONParser jsonParser = new JSONParser();
                    Object element = jsonParser.parse(result.getOkMsg());
                    if (element instanceof JSONArray) {
                        JSONArray jsonArray = (JSONArray) element;
                        int len = Integer.min(doneDisplayList.size(), jsonArray.size());
                        for (int i = 0; i < len; i++) {
                            TLSResult freshResult = new TLSResult(result.getRetCode(),
                                    jsonArray.get(i).toString(), result.getErrMsg(), result.getPrtMsg());
                            showOnComponent(getBlockMeta(doneDisplayList.get(i)), freshResult);
                        }
                    } else {
                        showOnComponent(getBlockMeta(doneDisplayList.get(0)), result);
                    }
                } catch (Exception e) {
                    getLogger(DefaultMsgSendAction.class.getName()).log(Level.WARNING,
                            "Exception encounters during successful json value parsing : \n{0}", e);
                }
            }

            CommonUtil.cachePrintMsg(result.getPrtMsg());
            if (result.getRedirection() != null) {
                String redirection = result.getRedirection();
                getLogger(DefaultMsgSendAction.class.getName()).log(Level.INFO,
                        "Be forced to jump to page[{0}], ignore designated page[{1}]",
                        new Object[] { redirection, String.valueOf(next.getNextPanel()) });
                int forwardPage = 0;
                try {
                    forwardPage = Integer.parseInt(redirection);
                } catch (Exception e) {
                    panelJump(next.getNextPanel());
                    return;
                }
                Object blockMeta = getBlockMeta(forwardPage);
                if (blockMeta == null || !(blockMeta instanceof Panel)) {
                    getLogger(DefaultMsgSendAction.class.getName()).log(Level.INFO,
                            "Be forced to jump to page[{0}], but page[{0}] is invalid Panel type. It is type[{1}]",
                            new Object[] { redirection,
                                    (blockMeta == null ? "NULL" : blockMeta.getClass().getSimpleName()) });
                    panelJump(next.getNextPanel());
                    return;
                }
                panelJump(forwardPage);
            } else {
                panelJump(next.getNextPanel());

                //trigger the action denoted in sendMsgAction
                Integer trigger = next.getTrigger();
                if (trigger != null && getBlockMeta(trigger) != null) {
                    final Component blockMeta = (Component) getBlockMeta(trigger);
                    switch (blockMeta.getType()) {
                    case COMBO_BOX:
                        JComboBox comboBlock = getBlock(trigger, JComboBox.class);
                        ItemListener[] itemListeners = comboBlock.getItemListeners();
                        ItemEvent e = new ItemEvent(comboBlock, ItemEvent.ITEM_STATE_CHANGED,
                                comboBlock.getItemAt(comboBlock.getItemCount() - 1).toString(),
                                ItemEvent.SELECTED);
                        for (ItemListener itemListener : itemListeners) {
                            itemListener.itemStateChanged(e);//wait and hang on util progress dialog gets to dispose
                        }
                        if (comboBlock.isEditable()) {
                            String value = comboBlock.getEditor().getItem().toString();
                            if (value.length() == 0) {
                                return;
                            }
                        }
                        break;
                    case BUTTON:
                        JButton btnBlock = getBlock(trigger, JButton.class);
                        btnBlock.doClick();
                        break;
                    default:
                        getLogger(DefaultMsgSendAction.class.getName()).log(Level.WARNING,
                                "Invalid trigger component[{0}] as unsupported type[{1}]",
                                new Object[] { trigger, blockMeta.getType() });
                        break;
                    }
                } //trigger dealing 
                  //keep next cursor on correct component
                Integer nextCursor = next.getNextCursor();
                if (nextCursor != null && getBlockMeta(nextCursor) != null) {
                    JComponent block = getBlock(nextCursor, JComponent.class);
                    block.requestFocusInWindow();
                }
            } // no redirection branch
        }
    });
}

From source file:org.nuclos.client.common.Utils.java

/**
 * sets the input focus to a certain collectable component in a LayoutML mask.
 * @param eafnInitialFocus entity name and field name of the component that is to receive to focus. The entity name is
 * null, if the component is not in a subform.
 * @param clctcompprovider map of all collectable components in the layout
 * @param mpsubformctl map of all subformcontrollers
 * @param frame frame of the layout (for possible warning dialogs only)
 * @param bShowWarnings displays warnings for components not found for focussing
 * @precondition eafnInitialFocus != null
 *//*from www .  j a v  a2 s  .c om*/
public static void setInitialComponentFocus(EntityAndFieldName eafnInitialFocus,
        final CollectableComponentsProvider clctcompprovider,
        final Map<String, ? extends SubFormController> mpsubformctl, final MainFrameTab frame,
        final boolean bShowWarnings) {

    final String sInitialFocusEntityName = eafnInitialFocus.getEntityName();
    final String sInitialFocusFieldName = eafnInitialFocus.getFieldName();

    // Must be invoked later, else focus is not set with compound components like LOVs
    EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            try {
                if (sInitialFocusEntityName == null) {
                    if (sInitialFocusFieldName != null) {
                        final Collection<CollectableComponent> collclctcomp = clctcompprovider
                                .getCollectableComponentsFor(sInitialFocusFieldName);
                        if (collclctcomp.isEmpty()) {
                            if (bShowWarnings) {
                                final String sMessage = SpringLocaleDelegate.getInstance().getMessage(
                                        "ClientUtils.1",
                                        "Das angegebene Feld f\u00fcr den initialen Fokus existiert nicht.");
                                JOptionPane
                                        .showMessageDialog(
                                                frame, sMessage, SpringLocaleDelegate.getInstance()
                                                        .getMessage("ClientUtils.2", "Hinweis"),
                                                JOptionPane.WARNING_MESSAGE);
                            }
                        } else {
                            final CollectableComponent clctcomp = collclctcomp.iterator().next();
                            final JComponent compFocus = clctcomp.getFocusableComponent();
                            compFocus.requestFocusInWindow();
                        }
                    }
                } else {
                    final SubFormController subformctl = mpsubformctl.get(sInitialFocusEntityName);
                    if (subformctl != null) {
                        final SubForm.SubFormTableModel subformtblmdl = (SubForm.SubFormTableModel) subformctl
                                .getSubForm().getJTable().getModel();

                        final JTable tbl = subformctl.getSubForm().getJTable();
                        final int iColumn = tbl.convertColumnIndexToView(
                                subformtblmdl.findColumnByFieldName(sInitialFocusFieldName));
                        if (iColumn != -1) {
                            if (subformtblmdl.getRowCount() > 0) {
                                tbl.editCellAt(0, iColumn);

                                Component comp = tbl.getCellEditor().getTableCellEditorComponent(tbl,
                                        tbl.getValueAt(0, iColumn), true, 0, iColumn);

                                // Special case for multiline text editor components
                                if (comp instanceof JScrollPane) {
                                    comp = ((JScrollPane) comp).getViewport().getView();
                                }
                                comp.requestFocusInWindow();
                            }
                        } else {
                            if (bShowWarnings) {
                                final String sMessage = SpringLocaleDelegate.getInstance().getMessage(
                                        "ClientUtils.3",
                                        "Das angegebene Feld in der Entit\u00e4t f\u00fcr den initialen Fokus existiert nicht.");
                                JOptionPane
                                        .showMessageDialog(
                                                frame, sMessage, SpringLocaleDelegate.getInstance()
                                                        .getMessage("ClientUtils.2", "Hinweis"),
                                                JOptionPane.WARNING_MESSAGE);
                            }
                        }
                    } else {
                        if (bShowWarnings) {
                            final String sMessage = SpringLocaleDelegate.getInstance().getMessage(
                                    "ClientUtils.4",
                                    "Die angegebene Entit\u00e4t f\u00fcr den initialen Fokus existiert nicht.");
                            JOptionPane.showMessageDialog(frame, sMessage,
                                    SpringLocaleDelegate.getInstance().getMessage("ClientUtils.2", "Hinweis"),
                                    JOptionPane.WARNING_MESSAGE);
                        }
                    }
                }
            } catch (Exception e) {
                LOG.error("setInitialComponentFocus failed: " + e, e);
            }
        }
    });
}

From source file:org.nuclos.client.common.Utils.java

/**
 * sets the input focus to a certain collectable component in a LayoutML mask.
 * @param sFocusFieldName field name of the component that is to receive to focus.
 * @param clctcompprovider map of all collectable components in the layout
 * @param frame frame of the layout (for possible warning dialogs only)
 * @param bShowWarnings displays warnings for components not found for focussing
 * @precondition eafnInitialFocus != null
 *///w  w  w .  j  a  v a2s .c o  m
public static void setComponentFocus(final String sFocusFieldName,
        final CollectableComponentsProvider clctcompprovider, final MainFrameTab frame,
        final boolean bShowWarnings) {
    // Must be invoked later, else focus is not set with compound components like LOVs
    EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            try {
                if (sFocusFieldName != null) {
                    final Collection<CollectableComponent> collclctcomp = clctcompprovider
                            .getCollectableComponentsFor(sFocusFieldName);
                    if (collclctcomp.isEmpty()) {
                        if (bShowWarnings) {
                            final String sMessage = SpringLocaleDelegate.getInstance().getMessage(
                                    "ClientUtils.1",
                                    "Das angegebene Feld f\u00fcr den initialen Fokus existiert nicht.");
                            JOptionPane.showMessageDialog(frame, sMessage,
                                    SpringLocaleDelegate.getInstance().getMessage("ClientUtils.2", "Hinweis"),
                                    JOptionPane.WARNING_MESSAGE);
                        }
                    } else {
                        final CollectableComponent clctcomp = collclctcomp.iterator().next();
                        final JComponent compFocus = clctcomp.getFocusableComponent();
                        compFocus.requestFocusInWindow();
                    }
                }
            } catch (Exception e) {
                LOG.error("setComponentFocus failed: " + e, e);
            }
        }
    });
}