Java Swing How to - Set Button text Mnemonic, one character in a label appears underlined








Question

We would like to know how to set Button text Mnemonic, one character in a label appears underlined.

Answer

/*w  ww  . j  av  a  2s  . c  om*/
import java.awt.event.KeyEvent;

import javax.swing.JButton;
import javax.swing.JOptionPane;
public class Main {
  public static void main(final String args[]) {
    JButton button = new JButton("Text Button");
    button.setMnemonic(KeyEvent.VK_B);

    JOptionPane.showMessageDialog(null, button);
  }
}