title: create hot keys for JButton in java using swing
body: <p>I use the following code to create hot keys for the java form using swing. If I press ALT+N,ALT+R,ALT+1,ALT+2 the cursor moves to correct text fields and I enter the value in corresponding Text Fields. It works properly. My problem is, I have Save and exit JButtons in this form if. I press CTRL+S means the Save button will be selected at the same time If i press CTRL+X means the exit button will be selected. How to create mnemonics for JButton? How to do CTRL+S,CTRL+X this using the following code?</p>
  
  <p>Thanks in advance.</p>
  
  <pre><code>package hotkeys;
  import java.awt.event.*;
  import javax.swing.*;
  import java.net.*;
  public class hotkey extends JFrame {
      public static void main(String arg[]) {
          JLabel Name = new JLabel("Name");
          JTextField tf1 = new JTextField(20);
          Name.setLabelFor(tf1);
          Name.setDisplayedMnemonic('N');
  
  
          JLabel Regno = new JLabel("RegNO");
          JTextField tf2 = new JTextField(20);
          Regno.setLabelFor(tf2);
          Regno.setDisplayedMnemonic('R');
  
          JLabel Mark1 = new JLabel("Mark1");
          JTextField tf3 = new JTextField(20);
          Mark1.setLabelFor(tf3);
          Mark1.setDisplayedMnemonic('1');
  
          JLabel Mark2 = new JLabel("Mark2");
          JTextField tf4 = new JTextField(20);
          Mark2.setLabelFor(tf4);
          Mark2.setDisplayedMnemonic('2');
  
  
          JButton b1 = new JButton("Save");
          JButton b2 = new JButton("eXit");
  
  
          JFrame f = new JFrame();
          JPanel p = new JPanel();
  
          p.add(Name);
          p.add(tf1);
          p.add(Regno);
          p.add(tf2);
          p.add(Mark1);
          p.add(tf3);
          p.add(Mark2);
          p.add(tf4);
          p.add(b1);
          p.add(b2);
  
          f.add(p);
          f.setVisible(true);
          f.pack();
      }
  }
  </code></pre>