Example usage for javax.swing JTextField requestFocus

List of usage examples for javax.swing JTextField requestFocus

Introduction

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

Prototype

public void requestFocus() 

Source Link

Document

Requests that this Component gets the input focus.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    final JTextField textfield = new JTextField(10);

    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            textfield.requestFocus();
        }/*from   w w w . ja  v  a 2s  .  c  om*/
    });
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    final JTextField textfield = new JTextField(10);

    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            textfield.requestFocus();
        }/* w w w  .jav  a 2 s  .c o  m*/
    });

}

From source file:KeyTextComponent.java

public static void main(String[] a) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(new KeyTextComponent(), BorderLayout.CENTER);
    JTextField field = new JTextField();
    field.setText("Click above blank area to request focus");
    frame.add(field, BorderLayout.SOUTH);
    frame.setSize(300, 100);//from   ww  w . java2s  .  c  o m
    frame.setVisible(true);
    field.requestFocus();
}

From source file:Main.java

public static void main(String[] args) {
    JTextField tf = new JTextField("mm");
    tf.setPreferredSize(tf.getPreferredSize());
    tf.setText("");

    JPanel pHacked = new JPanel();
    pHacked.add(tf);//  w  w w  .j  a  v  a 2  s  .c  om

    JPanel pStock = new JPanel();
    pStock.add(new JTextField(2));

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new java.awt.GridLayout(0, 1));
    frame.add(pHacked);
    frame.add(pStock);
    frame.setSize(150, 150);
    frame.setVisible(true);
    tf.requestFocus();
}

From source file:Main.java

public static void main(String[] args) {

    final JTextField tf = new JTextField("press <enter>", 20);
    tf.setHorizontalAlignment(JTextField.RIGHT);

    tf.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            int old = tf.getHorizontalAlignment();
            if (old == JTextField.LEFT)
                tf.setHorizontalAlignment(JTextField.RIGHT);
            if (old == JTextField.RIGHT)
                tf.setHorizontalAlignment(JTextField.CENTER);
            if (old == JTextField.CENTER)
                tf.setHorizontalAlignment(JTextField.LEFT);
        }/*from   w ww . j  a  va  2 s  . c  om*/
    });

    JFrame frame = new JFrame("JTextFieldExample");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(new java.awt.FlowLayout());
    frame.getContentPane().add(tf);
    frame.setSize(275, 75);
    frame.setVisible(true);
    tf.requestFocus();
}

From source file:Main.java

public static void focusAndSelectTextInTextField(JTextField textField) {
    int length = textField.getText().length();

    textField.setSelectionStart(0);//from   w  ww.j a v  a 2  s.  c o  m
    textField.setSelectionEnd(length);

    textField.requestFocus();
}

From source file:kenh.xscript.elements.Debug.java

private void initial(Container c) {
    c.setLayout(new BorderLayout());

    // Add variable list

    DefaultListModel<String> model = new DefaultListModel();

    if (this.getEnvironment() != null) {
        java.util.Set<String> keys = this.getEnvironment().getVariables().keySet();
        for (String key : keys) {
            model.addElement(key);//from   w  w  w  . ja  va 2 s  .  co  m
        }
    } else {
        for (int i = 1; i < 10; i++) {
            model.addElement("Variable " + i);
        }
        model.addElement("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
    }

    JList list = new JList(model);
    list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    JScrollPane listPane = new JScrollPane();
    listPane.setViewportView(list);
    listPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

    c.add(listPane, BorderLayout.EAST);

    list.setPreferredSize(new Dimension(150, list.getPreferredSize().height));

    // 

    JTextField quote = new JTextField();
    quote.requestFocus();

    //JButton button = new JButton(">>");

    JPanel quotePanel = new JPanel();
    quotePanel.setLayout(new BorderLayout());
    quotePanel.add(quote, BorderLayout.CENTER);
    //quotePanel.add(button, BorderLayout.EAST);

    JTextArea result = new JTextArea();
    result.setEditable(false);

    JScrollPane resultPane = new JScrollPane();
    resultPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    resultPane.setViewportView(result);

    JPanel panel = new JPanel();
    panel.setLayout(new BorderLayout());
    panel.add(quotePanel, BorderLayout.NORTH);
    panel.add(resultPane, BorderLayout.CENTER);

    c.add(panel, BorderLayout.CENTER);

    list.addListSelectionListener(this);
    //button.addActionListener(this);
    quote.addKeyListener(this);

    this.result = result;
}

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

public void showWindow() {
    if (!actionListenersInit)
        setupActionListeners();/*  w ww  . j a va2 s  .  co m*/
    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:SimpleAuthenticator.java

protected PasswordAuthentication getPasswordAuthentication() {

    // given a prompt?
    String prompt = getRequestingPrompt();
    if (prompt == null)
        prompt = "Please login...";

    // protocol/* w w  w  . j  a  v a 2 s  .  com*/
    String protocol = getRequestingProtocol();
    if (protocol == null)
        protocol = "Unknown protocol";

    // get the host
    String host = null;
    InetAddress inet = getRequestingSite();
    if (inet != null)
        host = inet.getHostName();
    if (host == null)
        host = "Unknown host";

    // port
    String port = "";
    int portnum = getRequestingPort();
    if (portnum != -1)
        port = ", port " + portnum + " ";

    // Build the info string
    String info = "Connecting to " + protocol + " mail service on host " + host + port;

    //JPanel d = new JPanel();
    // XXX - for some reason using a JPanel here causes JOptionPane
    // to display incorrectly, so we workaround the problem using
    // an anonymous JComponent.
    JComponent d = new JComponent() {
    };

    GridBagLayout gb = new GridBagLayout();
    GridBagConstraints c = new GridBagConstraints();
    d.setLayout(gb);
    c.insets = new Insets(2, 2, 2, 2);

    c.anchor = GridBagConstraints.WEST;
    c.gridwidth = GridBagConstraints.REMAINDER;
    c.weightx = 0.0;
    d.add(constrain(new JLabel(info), gb, c));
    d.add(constrain(new JLabel(prompt), gb, c));

    c.gridwidth = 1;
    c.anchor = GridBagConstraints.EAST;
    c.fill = GridBagConstraints.NONE;
    c.weightx = 0.0;
    d.add(constrain(new JLabel("Username:"), gb, c));

    c.anchor = GridBagConstraints.EAST;
    c.fill = GridBagConstraints.HORIZONTAL;
    c.gridwidth = GridBagConstraints.REMAINDER;
    c.weightx = 1.0;
    String user = getDefaultUserName();
    JTextField username = new JTextField(user, 20);
    d.add(constrain(username, gb, c));

    c.gridwidth = 1;
    c.fill = GridBagConstraints.NONE;
    c.anchor = GridBagConstraints.EAST;
    c.weightx = 0.0;
    d.add(constrain(new JLabel("Password:"), gb, c));

    c.anchor = GridBagConstraints.EAST;
    c.fill = GridBagConstraints.HORIZONTAL;
    c.gridwidth = GridBagConstraints.REMAINDER;
    c.weightx = 1.0;
    JPasswordField password = new JPasswordField("", 20);
    d.add(constrain(password, gb, c));
    // XXX - following doesn't work
    if (user != null && user.length() > 0)
        password.requestFocus();
    else
        username.requestFocus();

    int result = JOptionPane.showConfirmDialog(frame, d, "Login", JOptionPane.OK_CANCEL_OPTION,
            JOptionPane.QUESTION_MESSAGE);

    if (result == JOptionPane.OK_OPTION)
        return new PasswordAuthentication(username.getText(), password.getText());
    else
        return null;
}

From source file:com.qframework.core.GameonApp.java

private void onTextInput(final String resptype, final String respdata) {

    Timer t = new javax.swing.Timer(50, new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            JTextField area = new JTextField(resptype);
            area.setFocusable(true);//from w w w  . ja v a2s .c o  m
            area.requestFocus();
            Object options[] = new Object[] { area };
            Frame parent = null;
            if (mAppContext != null)
                parent = (Frame) SwingUtilities.getAncestorOfClass(java.awt.Frame.class, mAppContext);
            else
                parent = (Frame) SwingUtilities.getAncestorOfClass(java.awt.Frame.class, mAppletContext);

            int option = JOptionPane.showOptionDialog(parent, options, "", JOptionPane.YES_OPTION,
                    JOptionPane.INFORMATION_MESSAGE, null, null, null);
            if (option == JOptionPane.YES_OPTION) {
                String truncated = area.getText().replaceAll("[^A-Za-z0-9:.@ ]", "");
                String script = respdata + "('" + truncated + "' , 1);";
                mScript.execScript(script);
            } else {
                String script = respdata + "(undefined, 1);";
                mScript.execScript(script);

            }
        }
    });
    t.setRepeats(false);
    t.start();

}