Example usage for javax.swing JTextField JTextField

List of usage examples for javax.swing JTextField JTextField

Introduction

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

Prototype

public JTextField(int columns) 

Source Link

Document

Constructs a new empty TextField with the specified number of columns.

Usage

From source file:Main.java

public Main() {
    JLabel label = new JLabel("Text Field");
    JTextField textField = new JTextField(20);
    JRadioButton rb1 = new JRadioButton("Radio 1");
    JRadioButton rb2 = new JRadioButton("Radio 2");
    JButton button = new JButton("Button");

    JPanel panel1 = new JPanel();
    panel1.add(label);//from  ww  w . ja v  a 2 s. c  o m
    panel1.add(textField);

    JPanel panel2 = new JPanel();
    panel2.add(rb1);
    panel2.add(rb2);

    JPanel panel3 = new JPanel(new FlowLayout(FlowLayout.TRAILING));
    panel3.add(button);

    JPanel panel4 = new JPanel(new GridLayout(3, 1));
    panel4.add(panel1);
    panel4.add(panel2);
    panel4.add(panel3);

    setLayout(new GridBagLayout());
    add(panel4);
}

From source file:Main.java

public Main() {
    f.setBounds(50, 50, 300, 300);//from  ww w  .j a  v  a  2 s. c  o  m

    c = f.getContentPane();
    c.setLayout(new FlowLayout());

    btn = new JButton("OK");
    tf = new JTextField(20);

    c.add(btn);
    c.add(tf);

    f.setVisible(true);
    f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
}

From source file:MainClass.java

MainClass() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel p = new JPanel();
    p.setPreferredSize(new Dimension(300, 50));
    JTextField jtf = new JTextField(20);
    jtf.setPreferredSize(new Dimension(100, 20));
    p.add(jtf);/*from  w  w  w . ja v a2  s  .c o m*/

    getContentPane().add(p);

    pack();
    setVisible(true);
}

From source file:Main.java

public Main() {
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    JPanel pnl = new JPanel();
    pnl.add(new JLabel("Enter text"));
    final JTextField txtText;
    txtText = new JTextField("to be removed");
    pnl.add(txtText);//w ww  .j  a  va  2  s. com

    JButton btnRemove = new JButton("Remove");
    ActionListener al = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            String text = txtText.getText();
            text = Normalizer.normalize(text, Normalizer.Form.NFD);
            txtText.setText(text.replaceAll("[^\\p{ASCII}]", ""));
        }
    };
    btnRemove.addActionListener(al);
    pnl.add(btnRemove);
    add(pnl);
    pack();
    setVisible(true);
}

From source file:Main.java

public Main() {
    tabbedPane.setPreferredSize(new Dimension(300, 200));
    getContentPane().add(tabbedPane);/*from  www. j  a  va  2s .  com*/
    JPanel panel = new JPanel();
    tabbedPane.add(panel, "null");
    JTextField one = new JTextField("one");
    tabbedPane.add(one, "one");
    JTextField two = new JTextField("two");
    tabbedPane.add(two, "<html> Tittle  1 </html>");
    tabbedPane.setEnabledAt(2, false);
    tabbedPane.setTitleAt(2, "<html><font color=" + (tabbedPane.isEnabledAt(2) ? "black" : "red") + ">"
            + tabbedPane.getTitleAt(2) + "</font></html>");
    tabbedPane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
    tabbedPane.setTabPlacement(JTabbedPane.BOTTOM);
}

From source file:Main.java

/** 
 * Adds a <code>JTextField</code> with the specified text as a label
 * appearing before the text field to the given <code>JComponent</code>. 
 * Assuming the <code>JComponent</code> has the MigLayout set, the label
 * and text field are both left aligned within the <code>JComponent</code>.
 * If the wrap parameter is set to true, MigLayout's "wrap" attribute will be
 * applied to the <code>JTextField</code>, meaning the next component added
 * will appear on the next line.//from ww  w  . j  a va  2s.  co  m
 * (Exception: Will not work if MigLayout's flowy layout constraint is applied,
 * but it is rarely used MigLayout feature and thus not a common concern; however
 * if you have set this layout constraint on your <code>JComponent</code> do not
 * attempt to use the wrap option of this method.)
 * @param label - the text to appear in front of the text field
 * @param length - the length in columns of the text field 
 * @param wrap - indicates is this component should be the last on the current line
 * @return the <code>JTextField</code> added to the component
 */
public static JTextField addLabeledTextField(JComponent c, String label, int length, boolean wrap) {
    JLabel l = new JLabel(label);
    c.add(l, "align left");

    JTextField text = new JTextField(length);
    if (wrap) {
        c.add(text, "align left, wrap");
    } else {
        c.add(text, "align left");
    }

    return text;
}

From source file:RemoveAccents.java

public RemoveAccents() {
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    JPanel pnl = new JPanel();
    pnl.add(new JLabel("Enter text"));
    final JTextField txtText;
    txtText = new JTextField("to be removed");
    pnl.add(txtText);//from  ww  w  .ja va 2  s  .  c  o m

    JButton btnRemove = new JButton("Remove");
    ActionListener al;
    al = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            String text = txtText.getText();
            text = Normalizer.normalize(text, Normalizer.Form.NFD);
            txtText.setText(text.replaceAll("[^\\p{ASCII}]", ""));
        }
    };
    btnRemove.addActionListener(al);
    pnl.add(btnRemove);

    getContentPane().add(pnl);

    pack();
    setVisible(true);
}

From source file:MainClass.java

MainClass(String title) {
    super(title);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel jp = new JPanel();

    JLabel jl = new JLabel("Name:");
    jp.add(jl);//from   w w  w  .  j  a  v  a2  s .  c om

    JTextField jt = new JTextField(20);
    jp.add(jt);

    KeyStroke ks = KeyStroke.getKeyStroke(KeyEvent.VK_B, Event.CTRL_MASK);

    jt.getInputMap().put(ks, DefaultEditorKit.beepAction);

    getContentPane().add(jp);

    pack();
    setVisible(true);
}

From source file:Main.java

public Main() {
    JTextField textField = new JTextField("A TextField");
    textField.addFocusListener(this);
    JLabel label = new JLabel("A Label");
    label.addFocusListener(this);
    add(label);// w ww .ja  v  a  2 s. c o  m

    String comboPrefix = "Item #";
    final int numItems = 15;
    Vector vector = new Vector(numItems);
    for (int i = 0; i < numItems; i++) {
        vector.addElement(comboPrefix + i);
    }
    JComboBox comboBox = new JComboBox(vector);
    comboBox.addFocusListener(this);
    add(comboBox);

    JButton button = new JButton("A Button");
    button.addFocusListener(this);
    add(button);

    JList list = new JList(vector);
    list.setSelectedIndex(1);
    list.addFocusListener(this);
    JScrollPane listScrollPane = new JScrollPane(list);

    listScrollPane.getVerticalScrollBar().setFocusable(false);
    listScrollPane.getHorizontalScrollBar().setFocusable(false);
    add(listScrollPane);

    setPreferredSize(new Dimension(450, 450));
}

From source file:Main.java

public Main() throws HeadlessException {
    setSize(400, 200);//from  w w  w .j  ava2s .  c  om
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(new FlowLayout(FlowLayout.LEFT));

    JLabel usernameLabel = new JLabel("Username: ");
    JLabel passwordLabel = new JLabel("Password: ");
    JTextField usernameField = new JTextField(20);
    JPasswordField passwordField = new JPasswordField(20);

    usernameLabel.setDisplayedMnemonic(KeyEvent.VK_U);
    usernameLabel.setLabelFor(usernameField);
    passwordLabel.setDisplayedMnemonic(KeyEvent.VK_P);
    passwordLabel.setLabelFor(passwordField);

    getContentPane().add(usernameLabel);
    getContentPane().add(usernameField);
    getContentPane().add(passwordLabel);
    getContentPane().add(passwordField);
}