title: Using Enter key to use JButton instead of just mouse click?
body: <p>How would one bind the "Enter" key on a keyboard to press a JButton? Currently trying to figure this out but have no idea on what to do.</p>
  
  <p>Here is my code for reference. What it's supposed to do is create a guessing game (which it does) but I want to add the ability to press enter to click the "Enter" button (jButtonEnter in this case).</p>
  
  <pre><code>package day21;
  import java.awt.*;
  import java.awt.event.*;
  //import java.applet.*;
  import javax.swing.*;
  //import javax.swing.border.*;
  
  public class Day21 extends JApplet implements ActionListener{
  
      /**
       * 
       */
      private static final long serialVersionUID = 1L;
  
      //All important Variables
      JTextArea outTextArea = new JTextArea(10,20);
      JScrollPane scroller = new JScrollPane(outTextArea);
      static int GuessMe = (int) Math.ceil(Math.random()*1000);//randomizes the number
      JPanel jPanelTop = new JPanel();
      JPanel jPanelMid = new JPanel();
      JPanel jPanelLow = new JPanel();
      JLabel jLabelTop = new JLabel("Guess a number between 1 and 1000");
      static JTextField jTextFieldInput = new JTextField("Guess Here",25);
      JButton jButtonEnter = new JButton("Enter");
      JButton jButtonReset = new JButton("Reset");
      JButton jButtonClose = new JButton("Close");
  
      public void init(){
  
          this.getContentPane().setBackground(Color.white);
          this.setSize(new Dimension(400, 120));
  
          //Top Panel
          jPanelTop.setBackground(Color.cyan);
          jPanelTop.setBorder(BorderFactory.createRaisedBevelBorder());
          jPanelTop.setPreferredSize(new Dimension(14, 40));
          jPanelTop.setToolTipText("Top Panel");
          this.getContentPane().add(jPanelTop, BorderLayout.NORTH);
          jPanelTop.add(jLabelTop);
  
          //Middle Panel
          jPanelMid.setBackground(Color.orange);
          jPanelMid.setBorder(BorderFactory.createRaisedBevelBorder());
          jPanelMid.setPreferredSize(new Dimension(14, 40));
          jPanelMid.setToolTipText("Center Panel");
          this.getContentPane().add(jPanelMid, BorderLayout.CENTER);
          jPanelMid.add(jTextFieldInput);
          jPanelMid.add(jButtonEnter);
          jButtonEnter.addActionListener(this);
  
          //Lower Panel
          jPanelLow.setBackground(Color.black);
          jPanelLow.setBorder(BorderFactory.createRaisedBevelBorder());
          jPanelLow.setPreferredSize(new Dimension(14, 40));
          jPanelLow.setToolTipText("Lower Panel");
          this.getContentPane().add(jPanelLow, BorderLayout.SOUTH);
          jPanelLow.add(jButtonReset);
          jPanelLow.add(jButtonClose);
          jButtonClose.addActionListener(this);
          jButtonReset.addActionListener(this);
      }
      public void actionPerformed(ActionEvent e){
          double input = 0;
          boolean Error = false;
          String ErrorMSG = "Error: Please Enter a Number";
          try{
              input = Double.parseDouble(jTextFieldInput.getText().trim());;
          }
          catch(NumberFormatException n){
              Error = true;
          }
          if(Error){
              JOptionPane.showMessageDialog(rootPane, ErrorMSG); //If variable entered is not a number
          }
          String correctAnswer = "Hooray! You guessed Correctly! \n Press Reset to play again";
          String tooHigh = input + " is too high";
          String tooLow = input + " is too low";
          if(!Error){
              if(e.getSource() == jButtonEnter){
                  if (input == GuessMe){
                      jPanelLow.setBackground(Color.green);
                      JOptionPane.showMessageDialog(rootPane, correctAnswer);
                  }
                  if (input &gt; GuessMe){
                      jPanelLow.setBackground(Color.red);
                      JOptionPane.showMessageDialog(rootPane, tooHigh);
                  }
                  if (input &lt; GuessMe){
                      jPanelLow.setBackground(Color.red);
                      JOptionPane.showMessageDialog(rootPane, tooLow);
                  }
              }
          }
          if(e.getSource() == jButtonReset){ 
              Day21.reset(); //runs the reset() method which resets the window back to it's normal state
          }
          if(e.getSource() == jButtonClose){
              System.exit(1); //exits the program
          }
      }
  
      public static void reset(){
  
          GuessMe = (int) Math.ceil(Math.random()*1000);//randomizes the number
          jTextFieldInput.setText("Guess Here");
      }
  }
  </code></pre>