JButton Keyboard mnemonic - Java Swing

Java examples for Swing:JButton

Introduction

JButton supports keyboard mnemonic.

Keyboard mnemonic is a keyboard shortcut or keyboard indicator.

The following snippet of code sets C as a mnemonic key for a Close JButton:

import javax.swing.JButton;

public class Main {
  public static void main(String[] args) {
    JButton closeButton = new JButton("Close");

    // Set the 'C' key as mnemonic key for closeButton
    closeButton.setMnemonic('C');
  }
}

You can also use the following code to set a mnemonic key.

The KeyEvent class is in the java.awt.event package.

import java.awt.event.KeyEvent;

import javax.swing.JButton;

public class Main {
  public static void main(String[] args) {
    JButton closeButton = new JButton("Close");
    closeButton.setMnemonic(KeyEvent.VK_C);
  }
}

Related Tutorials