title: Focus on the button using tab key and press enter
body: <p>I have a simple jFrame with a submit button and a cancel button. Currently, everything works perfectly. User perform action on mouse click. Now I want to press 'tab' first to focus the button then it should fire whenever someone presses the enter key. It should work for both keys. What am I missing here? I am using netbean IDE. Below is a sample of the code that works perfectly using mouse click.</p>
  
  <pre><code> private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
          // TODO add your handling code here:
           String ws = null;
           if(unitno.getText().length() == 14)
          {
              String substr=unitno.getText().substring(unitno.getText().length()-4);
              if (substr.equals("0678"))
              {
                  model.setSelectedItem("Tesla");
              }
              else if(substr.equals("0675"))
              {
                   model.setSelectedItem("Weber Base");
              }
              else if(substr.equals("0676"))
              {
                   model.setSelectedItem("Weber Mid");
              }
                 else if(substr.equals("0677")|| substr.equals("067j")|| substr.equals("067J"))
              {
                   model.setSelectedItem("Weber PDL");
              }
          }
          try{
               Double wd = Double.parseDouble(w.getText());
               if(wd&lt;251.43 || wd&gt;253.03){
                    w.setForeground(Color.BLUE);
                    w.setBackground(Color.YELLOW);
                    wt.setForeground(Color.red);
               }
               else{
                     ws = w.getText();
                    w.setForeground(Color.BLACK);
                    w.setBackground(Color.WHITE);
               }
  
               if(ws ==null || unitno.getText().equals("")){
                  int dialogButton = JOptionPane.YES_NO_OPTION;
                  int dialogResult = JOptionPane.showConfirmDialog(this, "Empty field or out of spec is detected. Press YES to save and NO to edit.", "Error", dialogButton);
                  if(dialogResult ==0){
                      ws = w.getText();
  
                      w.setForeground(Color.BLACK);
                      w.setBackground(Color.WHITE);  
  
                      BufferedWriter output = null;
                      FileInputStream fs = null;
                      FileWriter fout = null;
  
                      try {
                          // TODO add your handling code here:
                          File desktop = new File(System.getProperty("user.home"), "Desktop");
                         // Double total = Double.parseDouble(ba) + Double.parseDouble(fr) ;
                          File dir = new File (desktop + "/WattCPC");
                          if(!dir.exists()){
                              dir.mkdirs();
                          }
                          File myFile = new File(desktop + "/WattCPC/topextimechwidth.txt");
                          if(!myFile.exists()) {
                              myFile.createNewFile();
                          } 
                          fs = new FileInputStream(desktop + "/WattCPC/topextimechwidth.txt");
                          BufferedReader br = new BufferedReader(new InputStreamReader(fs));
                          output = new BufferedWriter(new FileWriter(myFile,true));
                          PrintWriter fileout = new PrintWriter(output,true);
                          if (br.readLine() == null) {
                                 fileout.println("Model" + "," + "Date" + "," + "Line" + "," + "Shift" + "," + "Unit Number" + "," + "Width");
                           }
                          DateFormat dateFormat = new SimpleDateFormat("MMM/dd/yyyy hh:mm");
                          Date date = new Date();
                          for(int i = 1; i&lt;100; ++i){
                              String linez = br.readLine();
                              if(linez==null){
                                 fileout.println(model.getSelectedItem() + "," + dateFormat.format(date) + "," + line.getSelectedItem() + "," + shift.getSelectedItem() + "," + unitno.getText() + "," + ws  );
                                 break;
                              }
                          }        
                      } catch (IOException ex) {
                          Logger.getLogger(setup.class.getName()).log(Level.SEVERE, null, ex);
                      } finally {
                          try {
                              output.close();
                          } catch (IOException ex) {
                              Logger.getLogger(setup.class.getName()).log(Level.SEVERE, null, ex);
                          }
                      }
                      JOptionPane msg = new JOptionPane("Saved successfully", JOptionPane.INFORMATION_MESSAGE);
                      final JDialog dlg = msg.createDialog("Message");
                      dlg.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
                      new Thread(new Runnable() {    
  </code></pre>