Example usage for javax.swing JTextField addActionListener

List of usage examples for javax.swing JTextField addActionListener

Introduction

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

Prototype

public synchronized void addActionListener(ActionListener l) 

Source Link

Document

Adds the specified action listener to receive action events from this textfield.

Usage

From source file:Main.java

public static void main(String[] a) {
    JTextField jTextField1 = new JTextField();

    jTextField1.setText("java2s.com");

    jTextField1.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.out.println("action");
        }/* w  w w  . jav  a  2  s  . c o m*/
    });
    JOptionPane.showMessageDialog(null, jTextField1);
}

From source file:Main.java

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

    JTextField jTextField1 = new JTextField();

    jTextField1.setText("jTextField1");
    jTextField1.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.out.println("action");
        }/*ww  w  .  j a v a2 s  .c o  m*/
    });
    frame.add(jTextField1);

    frame.setSize(300, 200);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JComboBox<String> comboBox = new JComboBox<>(new String[] { "A", "B", "C" });
    comboBox.setEditable(true);//from  ww w  . j  a v a  2  s  .  c  o m

    JTextField editorComponent = (JTextField) comboBox.getEditor().getEditorComponent();
    editorComponent.addActionListener(e -> {
        editorComponent.transferFocus();
    });

    JPanel panel = new JPanel(new FlowLayout());
    panel.add(new JLabel("Field 1"));
    panel.add(comboBox);
    panel.add(new JLabel("Field 2"));
    panel.add(new JTextField(10));
    panel.add(new JLabel("Field 3"));
    panel.add(new JTextField(10));

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null);

    Container c = frame.getContentPane();
    c.setLayout(new BorderLayout());
    c.add(panel, BorderLayout.CENTER);
    frame.pack();
    frame.setVisible(true);
}

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());
            }/*www  . j a v a 2s .  c om*/
        }

        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());
            }//from   w  w  w  .j av a2  s .c o  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.show();
}

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 w w  .j a  v  a  2 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) {
    JFrame frame = new JFrame();

    String[] list = { "Hello 1", "Hello 2", "Hello 3", "Hello 4" };
    DefaultComboBoxModel<String> model = new DefaultComboBoxModel<>(list);
    JComboBox<String> comboBox = new JComboBox<>(model);
    frame.add(comboBox, BorderLayout.NORTH);

    final JTextField textField = new JTextField(30);
    frame.add(textField, BorderLayout.SOUTH);
    frame.add(new JLabel("Type something, then press enter", JLabel.CENTER));

    textField.addActionListener(ae -> {
        String text = textField.getText();
        model.addElement(text);//from ww  w  .  jav  a 2s.c  om
        comboBox.setSelectedItem(text);
        textField.setText("");

    });

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String args[]) {

    final JTextField textField = new JTextField();

    JScrollBar scrollBar = new JScrollBar(JScrollBar.HORIZONTAL);

    JPanel panel = new JPanel();
    panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

    BoundedRangeModel brm = textField.getHorizontalVisibility();
    scrollBar.setModel(brm);/* www . j  a  va2  s . com*/
    panel.add(textField);
    panel.add(scrollBar);

    textField.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.out.println("Text: " + textField.getText());
        }
    });

    JFrame frame = new JFrame("Text Slider");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.add(panel, BorderLayout.NORTH);
    frame.setSize(300, 100);
    frame.setVisible(true);
}

From source file:de.codesourcery.eve.skills.ui.model.FilteringTreeModel.java

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

    final FilteringTreeModel model = new FilteringTreeModel(createTreeModel());

    // setup text field
    final JTextField filterTextField = new JTextField(30);
    filterTextField.addActionListener(new ActionListener() {

        @Override// w ww  . j  a  v a  2  s. c  o m
        public void actionPerformed(ActionEvent e) {
            model.viewFilterChanged();
        }
    });

    // setup tree
    final JTree tree = new JTree();
    final IViewFilter<ITreeNode> filter = new AbstractViewFilter<ITreeNode>() {

        @Override
        public boolean isHiddenUnfiltered(ITreeNode node) {
            final String nodeValue = node.toString();
            final String expected = filterTextField.getText();

            final boolean isHidden = !StringUtils.isBlank(nodeValue) && !StringUtils.isBlank(expected)
                    && nodeValue.startsWith("child")
                    && !nodeValue.toLowerCase().contains(expected.toLowerCase());

            System.out.println(nodeValue + " does not match " + expected + " => " + isHidden);
            return isHidden;
        }
    };
    model.setViewFilter(filter);
    tree.setModel(model);

    // set 
    final JPanel panel = new JPanel();

    new GridLayoutBuilder()
            .add(new GridLayoutBuilder.HorizontalGroup(new GridLayoutBuilder.Cell(new JScrollPane(tree)),
                    new GridLayoutBuilder.FixedCell(filterTextField)))
            .addTo(panel);

    frame.getContentPane().add(panel);

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

From source file:TextSlider.java

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

    final JTextField textField = new JTextField();

    JScrollBar scrollBar = new JScrollBar(JScrollBar.HORIZONTAL);

    JPanel panel = new JPanel();
    panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

    BoundedRangeModel brm = textField.getHorizontalVisibility();
    scrollBar.setModel(brm);/*from  ww  w  .j  a  v  a 2s .c om*/
    panel.add(textField);
    panel.add(scrollBar);

    final TextSlider ts = new TextSlider();
    textField.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.out.println("Text: " + textField.getText());
        }
    });
    frame.add(panel, BorderLayout.NORTH);
    frame.setSize(300, 100);
    frame.setVisible(true);
}