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

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. If you run the program you will get an output telling you that the Num Lock button is active or not active.

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

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

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.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. This method
        // return boolean true value if it is "on".
        boolean isOn = Toolkit.getDefaultToolkit().getLockingKeyState(
                KeyEvent.VK_CAPS_LOCK);

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

How do I get my screen size?

package org.kodejava.awt;

import java.awt.*;

public class ScreenSizeExample {
    public static void main(String[] args) {
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

        System.out.println("Screen Width: " + screenSize.getWidth());
        System.out.println("Screen Height: " + screenSize.getHeight());
    }
}

The output of the code snippet above:

Screen Width: 2560.0
Screen Height: 1080.0

How do I make a centered JFrame?

If you have a JFrame in your Java Swing application and want to center its position on the screen you can use the following code snippets.

The first way is to utilize java.awt.Toolkit class to get the screen size. The getScreenSize() method return a java.awt.Dimension from where we can get the width and height of the screen. Having this values in hand we can calculate the top left position of our JFrame as shown in step 2 of the code below.

package org.kodejava.swing;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
import java.awt.Dimension;
import java.awt.Toolkit;

public class CenteredJFrame extends JFrame {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            // 1. Get the size of the screen
            Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

            CenteredJFrame frame = new CenteredJFrame();
            frame.setTitle("Centered JFrame");
            frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            frame.setSize(500, 500);

            // 2. Calculates the position where the CenteredJFrame
            // should be paced on the screen.
            int x = (screenSize.width - frame.getWidth()) / 2;
            int y = (screenSize.height - frame.getHeight()) / 2;
            frame.setLocation(x, y);
            frame.setVisible(true);
        });
    }
}

The second way which is better and simpler is to use the setLocationRelativeTo(Component) method. According to the Java API documentation of this method: If the component is null, or the GraphicsConfiguration associated with this component is null, the window is placed in the center of the screen.

If you call the JFrame.pack() method. The method should be called before the setLocationRelativeTo() method.

So we can rewrite the code above like this:

package org.kodejava.swing;

import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

public class CenteredJFrameSecond {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            CenteredJFrame frame = new CenteredJFrame();
            frame.setTitle("Centered JFrame");
            frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            frame.setSize(500, 500);

            // Place the window in the center of the screen.
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        });
    }
}