Example usage for javax.swing JTextField requestFocusInWindow

List of usage examples for javax.swing JTextField requestFocusInWindow

Introduction

In this page you can find the example usage for javax.swing JTextField 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 main(String[] args) {
    JTabbedPane tabbedPane;//from   w ww . j av  a  2 s  .c o  m
    JTextField txtFoo = new JTextField(10);
    JPanel pnlFoo = new JPanel();
    pnlFoo.add(new JButton("Button 1"));
    pnlFoo.add(new JLabel("Foo"));
    pnlFoo.add(txtFoo);

    JTextField txtBar = new JTextField(10);
    JPanel pnlBar = new JPanel();
    pnlBar.add(new JButton("Button 3"));
    pnlBar.add(new JLabel("Bar"));
    pnlBar.add(txtBar);

    tabbedPane = new JTabbedPane();
    tabbedPane.addTab("Tab 1", pnlFoo);
    tabbedPane.addTab("Tab 2", pnlBar);

    tabbedPane.addChangeListener(e -> {
        Component comp = tabbedPane.getSelectedComponent();
        if (comp.equals(pnlFoo)) {
            txtFoo.requestFocusInWindow();
        } else if (comp.equals(pnlBar)) {
            txtBar.requestFocusInWindow();
        }
    });

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(460, 200);
    frame.getContentPane().add(tabbedPane, BorderLayout.CENTER);
    frame.setVisible(true);

    txtFoo.requestFocusInWindow();
}

From source file:Main.java

/**
 * Same as {@link #requestFocus(Component)} but also selects all text in the text-field.
 *//*from  w w  w  .  j  a va  2 s  . c o  m*/
public static void requestFocusSelectAll(final JTextField c) {

    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            c.requestFocusInWindow();
            int l = c.getDocument().getLength();
            if (l > 0) {
                c.setSelectionStart(0);
                c.setSelectionEnd(l);
            }
        }
    });
}

From source file:com.gs.obevo.util.inputreader.DialogInputReader.java

@Override
public String readLine(String promptMessage) {
    final JTextField juf = new JTextField();
    JOptionPane juop = new JOptionPane(juf, JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_CANCEL_OPTION);
    JDialog userDialog = juop.createDialog(promptMessage);
    userDialog.addComponentListener(new ComponentAdapter() {
        @Override/*  w  ww. j a v a  2s  .  com*/
        public void componentShown(ComponentEvent e) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    juf.requestFocusInWindow();
                }
            });
        }
    });
    userDialog.setVisible(true);
    int uresult = (Integer) juop.getValue();
    userDialog.dispose();
    String userName = null;
    if (uresult == JOptionPane.OK_OPTION) {
        userName = new String(juf.getText());
    }

    if (StringUtils.isEmpty(userName)) {
        return null;
    } else {
        return userName;
    }
}

From source file:com.osparking.osparking.Settings_System.java

private boolean someIPaddressWrong() {
    InetAddressValidator validator = InetAddressValidator.getInstance();

    for (int i = 0; i < gateCount; i++) {
        JTextField txtField;

        for (DeviceType devType : DeviceType.values()) {
            String devName = devType.toString();
            txtField = (JTextField) getComponentByName(devName + (i + 1) + "_IP_TextField");
            if (!validator.isValidInet4Address(txtField.getText())) {
                GatesTabbedPane.setSelectedIndex(i);
                txtField.requestFocusInWindow();
                JOptionPane.showConfirmDialog(this,
                        devType.getContent() + " #" + (i + 1) + " " + IP_ADDR_ERROR_1.getContent()
                                + System.lineSeparator() + IP_ADDR_ERROR_2.getContent() + "127.0.0.1",
                        IP_ERROR_TITLE.getContent(), JOptionPane.PLAIN_MESSAGE, WARNING_MESSAGE);
                return true;
            }/* w  ww .ja  va 2s. com*/
        }
    }
    return false;
}

From source file:com.osparking.osparking.Settings_System.java

/**
 * Checks if natural number is entered and give warning when non-numeric entered.
 * @param textField field whose text is supposed to contain a number
 * @param dialogTitle title of the dialog box that will be shown when non-natural number string is entered
 * @return <b>true</b> when a natural number is in the <code>textField</code>, 
 * <b>false</b> otherwise//  ww w  .ja va  2  s  .c o  m
 */
private boolean TextFieldNumericValueOK(JTextField textField, String dialogTitle) {
    // Check if empty string or numeric 0 were entered. 
    String input = textField.getText().trim();

    input = removeNonNumeric(input);

    if (input.length() == 0 || Integer.parseInt(input) == 0) {
        JOptionPane.showConfirmDialog(this, "Enter a value of 1 or more ..", dialogTitle,
                JOptionPane.PLAIN_MESSAGE, WARNING_MESSAGE);
        textField.requestFocusInWindow();
        textField.select(0, input.length());
        return false;
    } else {
        return true;
    }
}