Is it possible to detect when someone presses Enter while typing in a JTextField in java? Without having to create a button and set it as the default.
10 Answers
A JTextField
was designed to use an ActionListener
just like a JButton
is. 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.
Also, an added benefit is that you can share the listener with a button even if you don't want to make the button a default button.
JButton button = new JButton("Do Something");
button.addActionListener( action );
Note, this example uses an Action
, which implements ActionListener
because Action
is a newer API with addition features. For example you could disable the Action
which would disable the event for both the text field and the button.

- 321,443
- 19
- 166
- 288
-
1Agreed. More elegant solution than mine, in the general case. – Kos Dec 12 '10 at 21:28
-
@camickr is an expert of Java swings – Mohamed Iqzas Jan 02 '17 at 09:56
-
Instead of adding the action to both, you can also call `button.doClick();` in the `textField.addActionListener()` method. – Mohammad Kholghi Aug 04 '21 at 11:12
JTextField function=new JTextField(8);
function.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
//statements!!!
}});
all you need to do is addActionListener to the JTextField like above! After you press Enter the action will performed what you want at the statement!

- 822
- 13
- 27
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.
}
}

- 109,027
- 88
- 289
- 474
-
-1 :( KeyListeners are way too low level from Swing's perspective. Use the API which is intended to be used with Swing :-) – nIcE cOw Aug 20 '13 at 05:47
-
For some reason this post is the most positive voted by anonymous users. :-) Yeah, this was my solution that I found. Thanks for comments! – Ionică Bizău Feb 24 '14 at 15:15
-
@nIcEcOw: Why is this incorrect? Doesn't the action listener have an ActionEvent input vs a KeyEvent input? How can you check if the ActionEvent was an Enter key press? The only relevant method seems to be getID(), which just states what type of action was performed... I'm confused. – rishimaharaj Mar 15 '14 at 05:18
-
Actually, I think I get it now -- the action for the JTextField only happens *on* an enter key press, right? – rishimaharaj Mar 15 '14 at 05:22
-
@rishimaharaj : If you will look at what Swing came out with to handle Key Events, is [Key Bindings](http://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html). This thingy does not suffer from focus related issues as opposed to __KeyListeners__. – nIcE cOw May 11 '14 at 18:49
-
1@nIcEcOw _KeyListeners are way too low level from Swing's perspective_: CITATION NEEDED. – guido May 02 '15 at 13:09
-
2@ᴳᵁᴵᴰᴼ: A very well documented thingy, can be found at the official tutorials of [KeyBindings](https://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html), though a very good [answer](http://stackoverflow.com/q/15290035/1057230) on stackoverflow, also covers the same aspect :-) – nIcE cOw May 02 '15 at 16:50
-
1@nIcEcOw that's exactly the doc page I had in mind. I find a lot of difference between suggesting to _prefer bindings for shortcuts and actions, only use listeners for controlling the keyboard at low-level_, and _don't use because it's the wrong api_. The doc page is not so black-and-white either. – guido May 03 '15 at 01:08
-
1@ᴳᵁᴵᴰᴼ: `KeyListeners` have many shortcomings, which are taken care of by to a greater extent by `KeyBindings`, such as, focus related, copy/paste related and many many more. It is to be avoided, for trivial tasks, such as, as asked in OP. – nIcE cOw May 03 '15 at 02:23
Do you want to do something like this ?
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();
}
}
});

- 654
- 6
- 7
The other answers (including the accepted ones) are good, but if you already use Java8, you can do the following (in a shorter, newer way):
textField.addActionListener(
ae -> {
//dostuff
}
);
As the accepted answer told, you can simply react with an ActionListener
, which catches the Enter-Key.
However, my approach takes benefit of the functional concepts which was introduced in Java 8.
If you want to use the same action for example for a button and the JTextField, you can do the following:
ActionListener l = ae -> {
//do stuff
}
button.addActionListener(l);
textField.addActionListener(l);
If further explaination is needed, please let me know!

- 1,456
- 1
- 15
- 27
First add action command on JButton or JTextField by:
JButton.setActionCommand("name of command");
JTextField.setActionCommand("name of command");
Then add ActionListener to both JTextField and JButton.
JButton.addActionListener(listener);
JTextField.addActionListener(listener);
After that, On you 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
}
}

- 9
- 1
For each text field in your frame, invoke the addKeyListener method. Then implement and override the keyPressed method, as others have indicated. Now you can press enter from any field in your frame to activate your action.
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_ENTER){
//perform action
}
}

- 19
- 1
If you want to set a default button action in a JTextField enter, you have to do this:
//put this after initComponents();
textField.addActionListener(button.getActionListeners()[0]);
It is [0] because a button can has a lot of actions, but normally just has one (ActionPerformed).

- 8,198
- 71
- 51
- 66

- 163
- 1
- 12
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 logic for 'Enter press' in JTextField
, it is better to keep logic inside the keyReleased()
block instead of keyTyped()
& keyPressed()
.

- 55,989
- 15
- 126
- 162

- 2,188
- 22
- 27
-
2-1 :( for repeating the same mistake again, which has already been down-voted in the previous answers, along with the specified reason for the same. `KeyListeners` are way too low level from `Swing`'s perspective. Use the API which is intended to be used with `Swing` :-) – nIcE cOw Aug 20 '13 at 05:44
Just use this code:
SwingUtilities.getRootPane(myButton).setDefaultButton(myButton);

- 27
- 3