How do I assign keyboard shortcut to JTabbedPane tabs?

To assign a keyboard shortcut for accessing JTabbedPane‘s tab you can use the setMnemonicAt(int tabIndex, int mnemonic) method of the JTabbedPane class. The tabIndex parameter is a zero-based value parameter which means that the first tab is at index number 0. For the mnenomic parameter you can use constants value defined in the java.awt.event.KeyEvent class.

Here is an example in action on how to assign keyboard shortcut to JTabbedPane‘s tabs. To access the tabs you can use the ALT + A, ALT + B, ALT + C and ALT + D keyboard combination.

package org.kodejava.swing;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;

public class TabbedPaneKeyboardShortcut extends JPanel {
    public TabbedPaneKeyboardShortcut() {
        initializeUI();
    }

    public static void showFrame() {
        JPanel panel = new TabbedPaneKeyboardShortcut();
        panel.setOpaque(true);

        JFrame frame = new JFrame("JTabbedPane Demo");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setContentPane(panel);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(TabbedPaneKeyboardShortcut::showFrame);
    }

    private void initializeUI() {
        this.setLayout(new BorderLayout());
        this.setPreferredSize(new Dimension(500, 200));

        JTabbedPane pane = new JTabbedPane();
        pane.addTab("A Tab", new JPanel());
        pane.addTab("B Tab", new JPanel());
        pane.addTab("C Tab", new JPanel());
        pane.addTab("D Tab", new JPanel());

        pane.setMnemonicAt(0, KeyEvent.VK_A);
        pane.setMnemonicAt(1, KeyEvent.VK_B);
        pane.setMnemonicAt(2, KeyEvent.VK_C);
        pane.setMnemonicAt(3, KeyEvent.VK_D);

        this.add(pane, BorderLayout.CENTER);
    }
}

The output of the code snippet above is:

JTabbedPane Tabs with Keyboard Shortcut

How do I turn on the Scroll Lock button?

The program below show you how to turn on the Scroll Lock button programmatically. Setting the locking state to Boolean.TRUE activate the Scroll Lock.

package org.kodejava.awt;

import java.awt.*;
import java.awt.event.KeyEvent;

public class TurnScrollLockOn {
    public static void main(String[] args) {
        // Gets the default toolkit.
        Toolkit toolkit = Toolkit.getDefaultToolkit();

        // Update the locking state for scroll lock button to true
        // will turn the scroll lock on.
        toolkit.setLockingKeyState(KeyEvent.VK_SCROLL_LOCK, Boolean.TRUE);
    }
}

How do I turn on the Num Lock button?

The example below show you how to activate the Num Lock button programmatically. Setting the locking state to Boolean.TRUE will turn the Num Lock on.

package org.kodejava.awt;

import java.awt.Toolkit;
import java.awt.event.KeyEvent;

public class TurnNumLockOn {
    public static void main(String[] args) {
        // Gets the default toolkit.
        Toolkit toolkit = Toolkit.getDefaultToolkit();

        // Update the locking state for num lock button to true
        // will turn the num lock on.
        toolkit.setLockingKeyState(KeyEvent.VK_NUM_LOCK, Boolean.TRUE);
    }
}

How do I turn on the Caps Lock button?

The code below show you how to activate the Caps Lock button programmatically. Setting the locking state to Boolean.TRUE will turn the Caps Lock on.

package org.kodejava.awt;

import java.awt.Toolkit;
import java.awt.event.KeyEvent;

public class TurnCapsLockOn {
    public static void main(String[] args) throws Exception {
        // Gets the default toolkit.
        Toolkit toolkit = Toolkit.getDefaultToolkit();

        // Update the locking state for caps lock button to true 
        // will turn the caps lock on.
        toolkit.setLockingKeyState(KeyEvent.VK_CAPS_LOCK, Boolean.TRUE);
    }
}

How do I read the status of Scroll Lock key?

This example show you how to detect if the Scroll Lock key is in active mode. If you run the program you will get an output telling you that the Scroll Lock button is active or not active.

package org.kodejava.awt;

import java.awt.Toolkit;
import java.awt.event.KeyEvent;

public class ScrollLockState {
    public static void main(String[] args) {
        Toolkit toolkit = Toolkit.getDefaultToolkit();

        // Get the locking state of the Scroll Lock button. This method 
        // return boolean true value if it is "on".
        boolean isOn = toolkit.getLockingKeyState(KeyEvent.VK_SCROLL_LOCK);

        System.out.println("ScrollLock button is " + (isOn ? "on" : "off"));
    }
}