Click input detection in JTextField

Is it possible to detect when someone presses Enter while entering a JTextField in java? Without the need to create a button and set it as the default.

+86
java swing jtextfield
Dec 11 '10 at 23:48
source share
9 answers

A JTextField was designed to use an ActionListener , like a JButton . See the addActionListener() Method of JTextField .

For example:

 Action action = new AbstractAction() { @Override public void actionPerformed(ActionEvent e) { System.out.println("some action"); } }; JTextField textField = new JTextField(10); textField.addActionListener( action ); 

Now the event is fired when the Enter key is used.

In addition, an additional advantage is that you can share the listener using the button, even if you do not want the button to be the default button.

 JButton button = new JButton("Do Something"); button.addActionListener( action ); 

Note. This example uses an Action that implements an ActionListener , because Action is a new API with additional features. For example, you can disable Action , which will disable the event for both the text field and the button.

+144
Dec 12 2018-12-12T00:
source share
 JTextField function=new JTextField(8); function.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ //statements!!! }}); 

all you have to do is addActionListener for JTextField as above! After pressing Enter, the action will do what you want in the instruction!

+20
May 29 '12 at 6:21
source share

Add an event for KeyPressed .

 private void jTextField1KeyPressed(java.awt.event.KeyEvent evt) { if(evt.getKeyCode() == KeyEvent.VK_ENTER) { // Enter was pressed. Your code goes here. } } 
+10
Jan 08 '13 at 8:15
source share

First add an action command to a JButton or JTextField:

 JButton.setActionCommand("name of command"); JTextField.setActionCommand("name of command"); 

Then add an ActionListener to the JTextField and JButton.

 JButton.addActionListener(listener); JTextField.addActionListener(listener); 

After that, in your ActionListener implementation write

 @Override public void actionPerformed(ActionEvent e) { String actionCommand = e.getActionCommand(); if(actionCommand.equals("Your actionCommand for JButton") || actionCommand.equals("Your actionCommand for press Enter")) { //Do something } } 
+1
Aug 05 '14 at 9:58
source share

Do you want to do something like that?

 JTextField mTextField = new JTextField(); mTextField.addKeyListener(new KeyAdapter() { @Override public void keyPressed(KeyEvent e) { if(e.getKeyCode() == KeyEvent.VK_ENTER){ // something like... //mTextField.getText(); // or... //mButton.doClick(); } } }); 
+1
May 23 '17 at 5:36
source share

Other answers (including accepted) are good, but if you are already using Java8, you can do the following (in short, newer):

 textField.addActionListener( ae -> { //dostuff } ); 

As the accepted answer said, you can simply respond with an ActionListener that catches Enter-Key.

However, my approach uses functional concepts that were introduced in Java 8.

If you want to use the same action, for example, for a button and JTextField, you can do the following:

 ActionListener l = ae -> { //do stuff } button.addActionListener(l); textField.addActionListener(l); 

If you require further information, please let me know!

+1
Aug 21 '17 at 21:16
source share

If you want to set the default button action in the JTextField input field, you must do this:

 //put this after initComponents(); textField.addActionListener(button.getActionListeners()[0]); 

This is [0] because a button can have many actions, but usually has one (ActionPerformed).

0
Oct 24 '15 at 5:19
source share
 public void keyReleased(KeyEvent e) { int key=e.getKeyCode(); if(e.getSource()==textField) { if(key==KeyEvent.VK_ENTER) { Toolkit.getDefaultToolkit().beep(); textField_1.requestFocusInWindow(); } } 

To write the logic for "Enter press" in a JTextField , it is better to save the logic inside the keyReleased() block instead of keyTyped() and keyPressed() .

-2
May 01 '13 at 12:52
source share

Just use this code:

 SwingUtilities.getRootPane(myButton).setDefaultButton(myButton); 
-2
Mar 14 '15 at 15:55
source share



All Articles