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.example.swing;

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

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

    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);
    }

    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(new Runnable() {
            public void run() {
                TabbedPaneKeyboardShortcut.showFrame();
            }
        });
    }
}

The output of the code snippet above is:

JTabbedPane Tabs with Keyboard Shortcut

How do I turn the Scroll Lock button on?

The program below show you how to turn the scroll lock button on programatically. Setting the locking state to Boolean.TRUE activate the scroll lock.

package org.kodejava.example.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 the Num Lock button on?

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.example.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 the Caps Lock button on?

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.example.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.

package org.kodejava.example.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. If it is "on"
        // this method return boolean true value.
        boolean isOn = toolkit.getLockingKeyState(KeyEvent.VK_SCROLL_LOCK);

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

If you run the program you will get an output telling you that the scroll lock button is active or not active.

How do I read the status of Num Lock key?

This example show you how to detect if the Num Lock key is in active mode.

package org.kodejava.example.awt;

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

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

        // Get the locking state of the Num Lock button. If it is "on"
        // this method return boolean true value.
        boolean isOn = toolkit.getLockingKeyState(KeyEvent.VK_NUM_LOCK);

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

If you run the program you will get an output telling you that the num lock button is active or not active.

How do I read the status of Caps Lock key?

This example show you how to detect if the Caps Lock key is in active mode.

package org.kodejava.example.awt;

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

public class CapsLockState {
    public static void main(String[] args) {
        // Get the locking state of the Caps Lock button. If it is "on" this method
        // return boolean true value.
        boolean isOn = Toolkit.getDefaultToolkit().getLockingKeyState(KeyEvent.VK_CAPS_LOCK);

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

How do I create key press event using Robot class?

In this example we use the java.awt.Robot class to generate a key press event. We can call the keyPress(int keyCode) method to produce this event.

package org.kodejava.example.awt;

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;

public class CreatingKeyboardEvent {
    public static void main(String[] args) {
        try {
            Robot robot = new Robot();

            // Create a three seconds delay.
            robot.delay(3000);

            // Generating key press event for writing the QWERTY letters
            robot.keyPress(KeyEvent.VK_Q);
            robot.keyPress(KeyEvent.VK_W);
            robot.keyPress(KeyEvent.VK_E);
            robot.keyPress(KeyEvent.VK_R);
            robot.keyPress(KeyEvent.VK_T);
            robot.keyPress(KeyEvent.VK_Y);
        } catch (AWTException e) {
            e.printStackTrace();
        }
    }
}