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() {
    JTextField textField = new JTextField("A TextField");
    textField.addFocusListener(this);
    JLabel label = new JLabel("A Label");
    label.addFocusListener(this);
    add(label);/*from   w  w  w  .ja  v a  2 s  .co m*/

    String comboPrefix = "ComboBox 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);

    String listPrefix = "List Item #";
    Vector listVector = new Vector(numItems);
    for (int i = 0; i < numItems; i++) {
        listVector.addElement(listPrefix + i);
    }
    JList list = new JList(listVector);
    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));
    setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
}

From source file:Test.java

public Test() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel p = new JPanel();
    p.add(new JButton("Button on Tab 0"));
    tabs.addTab("Tab 0", p);

    p = new JPanel();
    p.add(new JTextField("Text on Tab 1"));
    tabs.addTab("Tab 1", p);

    tabs.addChangeListener(new ChangeListener() {
        public void stateChanged(ChangeEvent e) {
            System.out.println("Tab=" + tabs.getSelectedIndex());
        }/*from  w ww  .j  a v  a 2s . c om*/
    });
    getContentPane().add(tabs, "Center");
    pack();
}

From source file:MainClass.java

MainClass() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel p = new JPanel();
    p.setPreferredSize(new Dimension(300, 50));
    JLabel jl = new JLabel("Name:");
    p.add(jl);//from  ww  w .  j  a va 2  s .  c om
    JTextField jtf = new JTextField(20);
    jtf.getAccessibleContext().setAccessibleName("Name-entry");
    p.add(jtf);

    AccessibleRelation ar = new AccessibleRelation("connector", jtf);

    AccessibleContext ac = jl.getAccessibleContext();
    ac.getAccessibleRelationSet().add(ar);

    getContentPane().add(p);

    pack();
    setVisible(true);
}

From source file:Main.java

public Main() {
    super();//from  w  w  w .ja  va2  s  .c  o m
    Container pane = getContentPane();
    pane.setLayout(new FlowLayout(FlowLayout.LEFT));
    pane.add(new JLabel("This is a test"));
    pane.add(new JButton("of a FlowLayout"));
    pane.add(new JTextField(30));
    pane.add(new JTextArea("This is a JTextArea", 3, 10));
    pane.add(new JLabel("This is a FlowLayout test with a long string"));
}

From source file:Main.java

public MyPanel() {
    JTextField labelA = new JTextField("Your A component");
    JTextField labelB = new JTextField("Your B component");
    JTextField labelC = new JTextField("Your C component");
    JTextField labelD = new JTextField("Top Right D");

    JPanel north = new JPanel(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.anchor = GridBagConstraints.FIRST_LINE_END;
    gbc.weightx = 1;//from w ww . j  a v a 2s .c o m
    gbc.insets = new Insets(10, 10, 10, 10);
    north.add(labelD, gbc);

    JPanel south = new JPanel(new GridBagLayout());
    gbc.anchor = GridBagConstraints.CENTER;
    gbc.gridy = 0;
    south.add(labelA, gbc);
    gbc.gridy = 1;
    south.add(labelB, gbc);
    gbc.gridy = 2;
    south.add(labelC, gbc);

    setLayout(new BorderLayout());
    add(north, BorderLayout.NORTH);
    add(south, BorderLayout.CENTER);
}

From source file:MainClass.java

public MainClass() {
    super();/*from   ww  w . jav  a  2  s  .  com*/
    JToolBar urlToolBar = new JToolBar();
    mURLField = new JTextField(40);
    urlToolBar.add(new JLabel("Location:"));
    urlToolBar.add(mURLField);
    frame.add(urlToolBar, BorderLayout.NORTH);

    mEditorPane = new JEditorPane();
    mEditorPane.setEditable(false);
    frame.add(new JScrollPane(mEditorPane), BorderLayout.CENTER);

    openURL("http://www.java2s.com");

    mURLField.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            openURL(ae.getActionCommand());
        }
    });

    mEditorPane.addHyperlinkListener(new LinkActivator());

    setSize(500, 600);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

From source file:Main.java

public Main() {
    super("JLayeredPane Demo");
    setSize(256, 256);//  w w w  .ja  v a 2s  .  c o m

    JPanel content = new JPanel();
    content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));
    content.setOpaque(false);

    JLabel label1 = new JLabel("Username:");
    label1.setForeground(Color.white);
    content.add(label1);

    JTextField field = new JTextField(15);
    content.add(field);

    JLabel label2 = new JLabel("Password:");
    label2.setForeground(Color.white);
    content.add(label2);

    JPasswordField fieldPass = new JPasswordField(15);
    content.add(fieldPass);

    setLayout(new FlowLayout());
    add(content);
    ((JPanel) getContentPane()).setOpaque(false);

    ImageIcon earth = new ImageIcon("largeJava2sLogo.png");
    JLabel backlabel = new JLabel(earth);
    getLayeredPane().add(backlabel, new Integer(Integer.MIN_VALUE));
    backlabel.setBounds(0, 0, earth.getIconWidth(), earth.getIconHeight());
    super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
}

From source file:Main.java

private JPanel createPanel() {
    JPanel p = new JPanel();
    p.setLayout(new GridBagLayout());

    GridBagConstraints c = new GridBagConstraints();
    c.gridx = 0;//from w  w w. ja  v  a2s  .c  om
    c.gridy = 0;
    c.anchor = GridBagConstraints.BASELINE_LEADING;
    p.add(new JLabel("Loan amount"), c);

    c.gridx++;
    p.add(new JTextField(15), c);

    c.gridx++;
    c.fill = GridBagConstraints.HORIZONTAL;
    c.weightx = 1.0;
    p.add(new JLabel("AUD"), c);

    c.gridx = 0;
    c.gridy++;
    c.fill = GridBagConstraints.NONE;
    c.weightx = 0.0;
    p.add(new JLabel("Loan term"), c);

    c.gridx++;
    p.add(new JTextField(15), c);

    c.gridx++;
    c.fill = GridBagConstraints.HORIZONTAL;
    c.weightx = 1.0;
    p.add(new JLabel("Years"), c);

    return p;
}

From source file:Main.java

public Main() {
    setPreferredSize(new Dimension(500, 500));
    getContentPane().setLayout(new BorderLayout());
    JPanel panel = new JPanel(new GridBagLayout());

    GridBagConstraints c = new GridBagConstraints();
    c.insets = new Insets(4, 4, 4, 4);
    panel.add(new JLabel("Label"), c);

    panel.add(new JTextField(20), c);

    c.gridwidth = GridBagConstraints.REMAINDER;
    panel.add(new JButton("btn"), c);
    c.gridwidth = 1;/*from  w w w.  j  a  v  a2 s  . co m*/

    panel.add(new JLabel("Name"), c);

    panel.add(new JTextField(20), c);

    c.gridwidth = GridBagConstraints.REMAINDER;
    panel.add(new JButton("btn"), c);
    c.weighty = 1.0;
    panel.add(Box.createGlue(), c);

    add(panel, BorderLayout.LINE_START);

    pack();
    setVisible(true);
}

From source file:Main.java

public Main() {
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    JButton openDialog = new JButton("Click here");
    JPanel myPanel = new JPanel();
    myPanel.add(new JButton(new AbstractAction("Click here") {
        @Override//from  w  w  w . j av a  2  s. co m
        public void actionPerformed(ActionEvent e) {
            JDialog dialog = new JDialog(myFrame, true);
            JTextField myField = new JTextField(10);
            JPanel innerPanel = new JPanel();
            innerPanel.add(myField);
            dialog.add(innerPanel);
            dialog.pack();
            dialog.setSize(new Dimension(160, 120));
            dialog.setLocationRelativeTo(myFrame);
            dialog.setVisible(true);
        }
    }));
    add(myPanel);
    pack();
    setSize(new Dimension(320, 240));
    setLocationRelativeTo(null);
    setVisible(true);
}