Example usage for java.awt Component requestFocusInWindow

List of usage examples for java.awt Component requestFocusInWindow

Introduction

In this page you can find the example usage for java.awt Component requestFocusInWindow.

Prototype

public boolean requestFocusInWindow() 

Source Link

Document

Requests that this Component get the input focus, if this Component's top-level ancestor is already the focused Window.

Usage

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  ww w . j a v  a 2 s .  co  m
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.ui.collect.SubForm.java

public void fireFocusGained() {
    AWTEvent event = EventQueue.getCurrentEvent();
    if (event instanceof KeyEvent) {
        if (getJTable().getModel().getRowCount() > 0) {
            getJTable().editCellAt(0, 0);
            getSubformTable().changeSelection(0, 0, false, false);
        } else if (getJTable().getModel().getRowCount() == 0) {
            for (FocusActionListener fal : getFocusActionLister()) {
                fal.focusAction(new EventObject(this));
                if (getJTable().editCellAt(0, 0)) {
                    SwingUtilities.invokeLater(new Runnable() {

                        @Override
                        public void run() {
                            Component editor = getJTable().getEditorComponent();
                            if (editor != null)
                                editor.requestFocusInWindow();
                        }//w  w w.ja  v  a2 s .  c  om
                    });

                }
            }
        }
    }
}

From source file:org.revager.tools.GUITools.java

/**
 * Creates a new standard table./*from   ww  w  . jav a2 s.c  o  m*/
 * 
 * @param model
 *            the table model
 * @param showHeader
 *            true if the header of the table should be visible
 * 
 * @return the newly created table
 */
@SuppressWarnings("serial")
public static JTable newStandardTable(TableModel model, boolean showHeader) {
    /*
     * Prep. for rollover
     */
    if (lastRolloverKey == Integer.MAX_VALUE) {
        lastRolloverKey = 0;
    } else {
        lastRolloverKey++;
    }

    final int keyIdx = lastRolloverKey;

    rollOverRowIndex.put(keyIdx, -1);

    final JTable table = new JTable(model) {

        @Override
        public boolean editCellAt(int row, int column, java.util.EventObject e) {
            boolean result = super.editCellAt(row, column, e);
            final Component editor = getEditorComponent();

            TableCellRenderer renderer = this.getColumnModel().getColumn(column).getCellRenderer();
            Font cellFont = null;
            if (renderer instanceof DefaultTableCellRenderer) {
                cellFont = ((DefaultTableCellRenderer) renderer).getFont();
            }
            if (editor != null && editor instanceof JTextComponent) {
                JTextComponent jTextComponent = (JTextComponent) editor;
                if (e == null) {
                    jTextComponent.selectAll();
                } else {
                    SwingUtilities.invokeLater(jTextComponent::selectAll);
                }
                jTextComponent.setBorder(UI.MARKED_BORDER_INLINE);
                if (cellFont != null) {
                    jTextComponent.setFont(cellFont);
                }
                editor.requestFocusInWindow();
            }
            return result;
        }

        @Override
        public TableCellRenderer getCellRenderer(int row, int column) {
            TableCellRenderer renderer = super.getCellRenderer(row, column);
            if (renderer instanceof DefaultTableCellRenderer) {
                ((DefaultTableCellRenderer) renderer).setBorder(new EmptyBorder(3, 3, 3, 3));
            }
            return renderer;
        }

        @Override
        public Component prepareRenderer(TableCellRenderer renderer, int row, int col) {
            Component comp = super.prepareRenderer(renderer, row, col);
            // Rollover
            comp.setBackground(getBackground());
            comp = super.prepareRenderer(renderer, row, col);
            if (!isRowSelected(row) && row == rollOverRowIndex.get(keyIdx)) {
                comp.setForeground(getForeground());
                comp.setBackground(UI.BLUE_BACKGROUND_COLOR);
            }

            // Tooltips
            JComponent jcomp = (JComponent) comp;
            if (renderer instanceof DefaultTableCellRenderer) {
                String toolTip = ((DefaultTableCellRenderer) renderer).getToolTipText();
                if (!StringUtils.isEmpty(toolTip)) {
                    jcomp.setToolTipText(toolTip);
                }
            }
            return comp;
        }
    };

    // Table properties
    table.setRowHeight(UI.TABLE_ROW_HEIGHT);
    table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    table.setShowGrid(false);
    table.setShowHorizontalLines(true);
    table.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));

    // Rollover
    MouseInputAdapter rolloverListener = new MouseInputAdapter() {
        @Override
        public void mouseExited(MouseEvent e) {
            rollOverRowIndex.put(keyIdx, -1);
            table.repaint();
        }

        @Override
        public void mouseMoved(MouseEvent e) {
            int row = table.rowAtPoint(e.getPoint());
            if (row != rollOverRowIndex.get(keyIdx)) {
                rollOverRowIndex.put(keyIdx, row);
                table.repaint();
            }
        }
    };
    table.addMouseMotionListener(rolloverListener);
    table.addMouseListener(rolloverListener);

    // Header
    if (!showHeader) {
        table.setTableHeader(null);
    }
    return table;
}