Java screen size: How to determine the screen/display size

Java screen size FAQ: How do I determine the size of the screen (or display) in pixels? (i.e., the screen size, or screen resolution)

The following Java code demonstrates how to get the screen size, using the Java Toolkit class:

// java - get screen size using the Toolkit class
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

Once you have the screen size as a Java Dimension object, you can get the height and width as follows:

// the screen height
screenSize.getHeight();

// the screen width
screenSize.getWidth();

As you might guess, the screen size height and width values are both pixel values (given as Java double values).

You can get the screen height and width in pixel values using the height and width fields (instead of the previously-shown methods), like this:

int screenHeight = screenSize.height;
int screenWidth = screenSize.width;