Learn how to retrieve these basic values from the Java AWT Toolkit easily.

If you need to obtain the screen dimensions for any reason, the easiest way to obtain it with Java is through the Toolkit class of AWT. This class is the abstract superclass of all actual implementations of the Abstract Window Toolkit. Subclasses of the Toolkit class are used to bind the various components to particular native toolkit implementations.

Many GUI events may be delivered to user asynchronously, if the opposite is not specified explicitly. As well as many GUI operations may be performed asynchronously. This fact means that if the state of a component is set, and then the state immediately queried, the returned value may not yet reflect the requested change.

The following example shows a pretty easy way to obtain these values:

package sandbox;

import java.awt.*;  

public class Sandbox {
     * Example of how to retrieve the screen dimensions and resolution.
     * @param args 
    public static void main(String[] args) {
        Toolkit t = Toolkit.getDefaultToolkit();    
        Dimension dimensions = t.getScreenSize();
        // Print values in the console
        System.out.println("Screen width: " + dimensions.width);
        System.out.println("Screen height: " + dimensions.height);
        System.out.println("Screen resolution: " + t.getScreenResolution());

Which generates an output according to your values like:

Screen resolution = 96
Screen width = 1920
Screen height = 1080

Happy coding !

Senior Software Engineer at Software Medico. Interested in programming since he was 14 years old, Carlos is a self-taught programmer and founder and author of most of the articles at Our Code World.