Example usage for javax.swing.text JTextComponent selectAll

List of usage examples for javax.swing.text JTextComponent selectAll

Introduction

In this page you can find the example usage for javax.swing.text JTextComponent selectAll.

Prototype

public void selectAll() 

Source Link

Document

Selects all the text in the TextComponent.

Usage

From source file:Main.java

public static void focusSelect(final JTextComponent tf) {
    tf.addFocusListener(new FocusAdapter() {
        public void focusGained(FocusEvent e) {
            tf.selectAll();
        }/*from  w  ww .ja  v a2 s. c o  m*/
    });
}

From source file:Main.java

/**
 * Installs a workaround for bug #4699955 in a JSpinner.
 * //from w w w  .ja v  a 2  s  .c o  m
 * @param spinner
 *            The spinner to fix
 */
public static void installSpinnerBugWorkaround(final JSpinner spinner) {
    ((DefaultEditor) spinner.getEditor()).getTextField().addFocusListener(new FocusAdapter() {
        @Override
        public void focusGained(final FocusEvent e) {
            if (e.getSource() instanceof JTextComponent) {
                final JTextComponent text = ((JTextComponent) e.getSource());
                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        text.selectAll();
                    }
                });
            }
        }
    });
    spinner.addFocusListener(new FocusAdapter() {
        @Override
        public void focusGained(final FocusEvent e) {
            if (e.getSource() instanceof JSpinner) {
                final JTextComponent text = ((DefaultEditor) ((JSpinner) e.getSource()).getEditor())
                        .getTextField();
                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        text.requestFocus();
                    }
                });
            }
        }
    });
}

From source file:Main.java

/**
 * Installs a workaround for bug #4699955 in a JSpinner.
 *
 * @param spinner/*from  ww  w. j ava 2  s .co  m*/
 *            The spinner to fix
 */

public static void installSpinnerBugWorkaround(final JSpinner spinner) {
    ((DefaultEditor) spinner.getEditor()).getTextField().addFocusListener(new FocusAdapter() {
        @Override
        public void focusGained(final FocusEvent e) {
            if (e.getSource() instanceof JTextComponent) {
                final JTextComponent text = (JTextComponent) e.getSource();
                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        text.selectAll();
                    }
                });
            }
        }
    });
    spinner.addFocusListener(new FocusAdapter() {
        @Override
        public void focusGained(final FocusEvent e) {
            if (e.getSource() instanceof JSpinner) {
                final JTextComponent text = ((DefaultEditor) ((JSpinner) e.getSource()).getEditor())
                        .getTextField();
                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        text.requestFocus();
                    }
                });
            }
        }
    });
}

From source file:ru.codemine.pos.ui.salespanel.QuantitySetupWindow.java

public void showWindow() {
    if (!actionListenersInit)
        setupActionListeners();//from   w w w .j av  a2s.c om
    mainWindow.blockBarcodeInput();

    Product product = salesPanel.getChequeSetupPanel().getSelectedProduct();
    if (product == null) {
        mainWindow.unblockBarcodeInput();
        return;
    }

    int maxQuantity = storeService.getAvaibleStocksOnRetail(product);
    onStoresLabel.setText("?: " + String.valueOf(maxQuantity) + " .");
    setTitle("? - " + product.getName());
    spinner.setModel(new SpinnerNumberModel(1, 1, maxQuantity, 1));

    JTextField ft = ((WebSpinner.DefaultEditor) spinner.getEditor()).getTextField();
    ft.addFocusListener(new FocusAdapter() {
        @Override
        public void focusGained(FocusEvent e) {
            final JTextComponent txtcomp = (JTextComponent) e.getSource();
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    txtcomp.selectAll();
                }
            });
        }
    });
    ft.requestFocus();

    setVisible(true);
}

From source file:org.apache.jmeter.testbeans.gui.ComboStringEditor.java

private void startEditing() {
    JTextComponent textField = (JTextComponent) combo.getEditor().getEditorComponent();

    combo.setEditable(true);//w  ww . j a  v  a2 s  .  com

    textField.requestFocusInWindow();
    String text = translate(initialEditValue);
    if (text == null) {
        text = ""; // will revert to last valid value if invalid
    }

    combo.setSelectedItem(text);

    int i = text.indexOf("${}");
    if (i != -1) {
        textField.setCaretPosition(i + 2);
    } else {
        textField.selectAll();
    }
}

From source file:org.executequery.gui.text.TextUtilities.java

public static void selectAll(JTextComponent textComponent) {
    textComponent.selectAll();
    //    textComponent.setCaretPosition(0);
    //    textComponent.moveCaretPosition(textComponent.getText().length());
}

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

/**
 * Creates a new standard table./*from  w  w w .  j  a  va2s .  co 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;
}