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(String text, int columns) 

Source Link

Document

Constructs a new TextField initialized with the specified text and columns.

Usage

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);
        }//  ww w . j ava2 s. co  m
    });

    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 main(String[] args) throws Exception {
    JTextField textField = new JTextField("Prefix_", 20);
    textField.setNavigationFilter(new NavigationFilterPrefixWithBackspace(7, textField));
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(textField);
    frame.pack();/* w  ww. ja va 2 s  .c om*/
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JPanel panel = new JPanel(new GridBagLayout());
    for (int i = 0; i < 25; i++) {
        JTextField field = new JTextField("Field " + i, 20);
        GridBagConstraints constraints = new GridBagConstraints();
        constraints.gridy = i;//from   ww  w .j ava2 s .c  om
        panel.add(field, constraints);
    }
    JScrollPane scrollPane = new JScrollPane(panel);
    JButton removeButton = new JButton("Remove Field");
    removeButton.addActionListener(e -> {
        if (panel.getComponentCount() >= 1) {
            panel.remove(panel.getComponentCount() - 1);
            scrollPane.revalidate();
            scrollPane.repaint();
        }
    });
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setSize(640, 480);
    f.setLocation(200, 200);
    f.getContentPane().add(scrollPane);
    f.getContentPane().add(removeButton, BorderLayout.SOUTH);
    f.setVisible(true);
}

From source file:BoxLayoutManagerContainerTest.java

public static void main(String[] args) {
    JFrame f = new JFrame("Vertical BoxLayout-managed container");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container pane = new BoxPanel();
    f.setContentPane(pane);// ww  w .  j  a v a2  s  .  c o  m
    pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));
    for (float align = 0.0f; align <= 1.0f; align += 0.25f) {
        JTextField tf = new JTextField("X Alignment = " + align, 10);
        tf.setAlignmentX(align);
        pane.add(tf);
    }
    f.setSize(400, 300);
    f.setVisible(true);
}

From source file:EditabilityExample.java

public static void main(String[] args) {
    try {/*w w w .j  a  v  a 2  s. co m*/
        UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
    } catch (Exception evt) {
    }

    JFrame f = new JFrame("Editability Example");
    f.getContentPane().setLayout(new BoxLayout(f.getContentPane(), BoxLayout.Y_AXIS));
    f.getContentPane().add(firstField);

    JTextField tf = new JTextField("A read-only text field", 20);
    tf.setEditable(false);
    f.getContentPane().add(tf);

    JTextArea ta = new JTextArea("An editable\ntext area", 2, 20);
    ta.setBorder(BorderFactory.createLoweredBevelBorder());
    f.getContentPane().add(ta);

    ta = new JTextArea("A read-only\ntext area", 2, 20);
    ta.setBorder(BorderFactory.createLoweredBevelBorder());
    ta.setEditable(false);
    f.getContentPane().add(ta);

    f.pack();
    f.show();

    if (args.length == 1 && args[0].equals("disable")) {
        // Toggle the enabled state of the first
        // text field every 10 seconds
        Timer t = new Timer(10000, new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                firstField.setEnabled(!firstField.isEnabled());
                firstField.setText(firstFieldText + (firstField.isEnabled() ? "" : " (disabled)"));
            }
        });
        t.start();
    }
}

From source file:FocusTest.java

public static void main(String args[]) {
    JFrame frame = new JFrame();
    Container contentPane = frame.getContentPane();

    FocusListener listener = new FocusListener() {
        public void focusGained(FocusEvent e) {
            dumpInfo(e);/* ww w .j  a va2 s.  com*/
        }

        public void focusLost(FocusEvent e) {
            dumpInfo(e);
        }

        private void dumpInfo(FocusEvent e) {
            System.out.println("Source  : " + name(e.getComponent()));
            System.out.println("Opposite : " + name(e.getOppositeComponent()));
            System.out.println("Temporary: " + e.isTemporary());
        }

        private String name(Component c) {
            return (c == null) ? null : c.getName();
        }
    };

    // First
    JPanel panel = new JPanel();
    JLabel label = new JLabel("Label 1: ");
    JTextField text = new JTextField("Type your text", 15);
    text.setName("First");
    text.addFocusListener(listener);
    label.setDisplayedMnemonic(KeyEvent.VK_1);
    label.setLabelFor(text);
    panel.add(label);
    panel.add(text);
    contentPane.add(panel, BorderLayout.NORTH);

    // Second
    panel = new JPanel();
    label = new JLabel("Label 2: ");
    text = new JTextField("14.0", 10);
    text.setName("Second");
    text.addFocusListener(listener);
    text.setHorizontalAlignment(JTextField.RIGHT);
    label.setDisplayedMnemonic(KeyEvent.VK_2);
    label.setLabelFor(text);
    panel.add(label);
    panel.add(text);
    contentPane.add(panel, BorderLayout.SOUTH);

    frame.pack();
    frame.show();
}

From source file:Main.java

public static void main(String args[]) {
    JFrame frame = new JFrame();
    Container contentPane = frame.getContentPane();
    ActionListener listener = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.out.println("Command: " + e.getActionCommand());
            int modifiers = e.getModifiers();
            System.out.println("\tALT : " + checkMod(modifiers, ActionEvent.ALT_MASK));
            System.out.println("\tCTRL : " + checkMod(modifiers, ActionEvent.CTRL_MASK));
            System.out.println("\tMETA : " + checkMod(modifiers, ActionEvent.META_MASK));
            System.out.println("\tSHIFT: " + checkMod(modifiers, ActionEvent.SHIFT_MASK));
            Object source = e.getSource();
            if (source instanceof JComboBox) {
                JComboBox jb = (JComboBox) source;
                System.out.println("Combo: " + jb.getSelectedItem());
            }//from  w  ww .  j  ava  2  s . co  m
        }

        private boolean checkMod(int modifiers, int mask) {
            return ((modifiers & mask) == mask);
        }
    };

    String flavors[] = { "Item 1", "Item 2", "Item 3" };
    JComboBox jc = new JComboBox(flavors);
    jc.setMaximumRowCount(4);
    jc.setEditable(true);
    jc.addActionListener(listener);
    contentPane.add(jc, BorderLayout.NORTH);

    JButton b = new JButton("Button!");
    b.addActionListener(listener);
    contentPane.add(b, BorderLayout.CENTER);

    JPanel panel = new JPanel();
    JLabel label = new JLabel("Label 1: ");
    JTextField text = new JTextField("Type your text", 15);
    text.addActionListener(listener);
    label.setDisplayedMnemonic(KeyEvent.VK_1);
    label.setLabelFor(text);
    panel.add(label);
    panel.add(text);
    contentPane.add(panel, BorderLayout.SOUTH);

    frame.pack();
    frame.setVisible(true);
}

From source file:ActionTest.java

public static void main(String args[]) {
    JFrame frame = new JFrame();
    Container contentPane = frame.getContentPane();
    ActionListener listener = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.out.println("Command: " + e.getActionCommand());
            System.out.println("Modifiers: ");
            int modifiers = e.getModifiers();
            System.out.println("\tALT : " + checkMod(modifiers, ActionEvent.ALT_MASK));
            System.out.println("\tCTRL : " + checkMod(modifiers, ActionEvent.CTRL_MASK));
            System.out.println("\tMETA : " + checkMod(modifiers, ActionEvent.META_MASK));
            System.out.println("\tSHIFT: " + checkMod(modifiers, ActionEvent.SHIFT_MASK));
            Object source = e.getSource();
            if (source instanceof JComboBox) {
                JComboBox jb = (JComboBox) source;
                System.out.println("Combo: " + jb.getSelectedItem());
            }// ww w. j  av  a 2  s . com
        }

        private boolean checkMod(int modifiers, int mask) {
            return ((modifiers & mask) == mask);
        }
    };

    String flavors[] = { "Item 1", "Item 2", "Item 3" };
    JComboBox jc = new JComboBox(flavors);
    jc.setMaximumRowCount(4);
    jc.setEditable(true);
    jc.addActionListener(listener);
    contentPane.add(jc, BorderLayout.NORTH);

    JButton b = new JButton("Button!");
    b.addActionListener(listener);
    contentPane.add(b, BorderLayout.CENTER);

    JPanel panel = new JPanel();
    JLabel label = new JLabel("Label 1: ");
    JTextField text = new JTextField("Type your text", 15);
    text.addActionListener(listener);
    label.setDisplayedMnemonic(KeyEvent.VK_1);
    label.setLabelFor(text);
    panel.add(label);
    panel.add(text);
    contentPane.add(panel, BorderLayout.SOUTH);

    frame.pack();
    frame.show();
}

From source file:SpringDemo2.java

public static void main(String[] args) {
    JFrame frame = new JFrame("SpringDemo2");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    // Set up the content pane.
    Container contentPane = frame.getContentPane();
    SpringLayout layout = new SpringLayout();
    contentPane.setLayout(layout);//from  ww  w  .  ja  va  2 s .  co  m

    // Create and add the components.
    JLabel label = new JLabel("Label: ");
    JTextField textField = new JTextField("Text field", 15);
    contentPane.add(label);
    contentPane.add(textField);

    // Adjust constraints for the label so it's at (5,5).
    layout.putConstraint(SpringLayout.WEST, label, 5, SpringLayout.WEST, contentPane);
    layout.putConstraint(SpringLayout.NORTH, label, 5, SpringLayout.NORTH, contentPane);

    // Adjust constraints for the text field so it's at
    // (<label's right edge> + 5, 5).
    layout.putConstraint(SpringLayout.WEST, textField, 5, SpringLayout.EAST, label);
    layout.putConstraint(SpringLayout.NORTH, textField, 5, SpringLayout.NORTH, contentPane);

    // Display the window.
    frame.pack();
    frame.setVisible(true);
}

From source file:SpringDemo3.java

public static void main(String[] args) {
    JFrame frame = new JFrame("SpringDemo3");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    // Set up the content pane.
    Container contentPane = frame.getContentPane();
    SpringLayout layout = new SpringLayout();
    contentPane.setLayout(layout);//from w w w. j  a va  2s.c o  m

    // Create and add the components.
    JLabel label = new JLabel("Label: ");
    JTextField textField = new JTextField("Text field", 15);
    contentPane.add(label);
    contentPane.add(textField);

    // Adjust constraints for the label so it's at (5,5).
    layout.putConstraint(SpringLayout.WEST, label, 5, SpringLayout.WEST, contentPane);
    layout.putConstraint(SpringLayout.NORTH, label, 5, SpringLayout.NORTH, contentPane);

    // Adjust constraints for the text field so it's at
    // (<label's right edge> + 5, 5).
    layout.putConstraint(SpringLayout.WEST, textField, 5, SpringLayout.EAST, label);
    layout.putConstraint(SpringLayout.NORTH, textField, 5, SpringLayout.NORTH, contentPane);

    // Adjust constraints for the content pane: Its right
    // edge should be 5 pixels beyond the text field's right
    // edge, and its bottom edge should be 5 pixels beyond
    // the bottom edge of the tallest component (which we'll
    // assume is textField).
    layout.putConstraint(SpringLayout.EAST, contentPane, 5, SpringLayout.EAST, textField);
    layout.putConstraint(SpringLayout.SOUTH, contentPane, 5, SpringLayout.SOUTH, textField);

    // Display the window.
    frame.pack();
    frame.setVisible(true);
}