Allowing the "Enter" key to press the submit button, as opposed to only using MouseClick
java, keylistener, swing
Solution
There is a simple trick for this. After you constructed the frame with all it buttons do this:
frame.getRootPane().setDefaultButton(submitButton);
For each frame, you can set a default button that will automatically listen to the Enter key (and maybe some other event's I'm not aware of). When you hit enter in that frame, the ActionListeners their `actionPerformed()` method will be invoked.
And the problem with your code as far as I see is that your dialog pops up every time you hit a key, because you didn't put it in the if-body. Try changing it to this:
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode()==KeyEvent.VK_ENTER){
System.out.println("Hello");
JOptionPane.showMessageDialog(null , "You've Submitted the name " + nameInput.getText());
}
}
UPDATE: I found what is wrong with your code. You are adding the key listener to the Submit button instead of to the TextField. Change your code to this:
SubmitButton listener = new SubmitButton(textBoxToEnterName);
textBoxToEnterName.addActionListener(listener);
submit.addKeyListener(listener);
Problem
I'm learning Swing class now and everything about it. I've got this toy program I've been putting together that prompts for a name and then presents a JOptionPane with the message "You've entered (Your Name)". The submit button I use can only be clicked on, but I'd like to get it to work with the Enter button too. I've tried adding a KeyListener, as is recommended in the Java book I'm using (Eventful Java, Bruce Danyluk and Murtagh). This is my code: ``` import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; public class NamePrompt extends JFrame{ private static final long serialVersionUID = 1L; String name; public NamePrompt(){ setLayout(new BorderLayout()); JLabel enterYourName = new JLabel("Enter Your Name Here:"); JTextField textBoxToEnterName = new JTextField(21); JPanel panelTop = new JPanel(); panelTop.add(enterYourName); panelTop.add(textBoxToEnterName); JButton submit = new JButton("Submit"); submit.addActionListener(new SubmitButton(textBoxToEnterName)); submit.addKeyListener(new SubmitButton(textBoxToEnterName)); JPanel panelBottom = new JPanel(); panelBottom.add(submit); //Add panelTop to JFrame add(panelTop, BorderLayout.NORTH); add(panelBottom, BorderLayout.SOUTH); //JFrame set-up setTitle("Name Prompt Program"); setDefaultCloseOperation(EXIT_ON_CLOSE); pack(); setLocationRelativeTo(null); } public static void main(String[] args) { NamePrompt promptForName = new NamePrompt(); promptForName.setVisible(true); } } ``` And this is the actionListener, keyListener class: ``` import java.awt.Component; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JTextField; public class SubmitButton implements ActionListener, KeyListener { JTextField nameInput; public SubmitButton(JTextField textfield){ nameInput = textfield; } @Override public void actionPerformed(ActionEvent submitClicked) { Component frame = new JFrame(); JOptionPane.showMessageDialog(frame , "You've Submitted the name " + nameInput.getText()); } @Override public void keyPressed(KeyEvent e) { if (e.getKeyCode()==KeyEvent.VK_ENTER){ System.out.println("Hello"); } Component frame = new JFrame(); JOptionPane.showMessageDialog(frame , "You've Submitted the name " + nameInput.getText()); } @Override public void keyReleased(KeyEvent arg0) { // TODO Auto-generated method stub } @Override public void keyTyped(KeyEvent arg0) { } } ```