devxlogo

Adjusting Java applications to screen resolution

Adjusting Java applications to screen resolution

Question:
We are developing a Java application which we want to distribute.We want our application main Frame/window to occupy the entire screen. But at runtime how do we adjust the size of our screen tothe resolution at the clients machine. Is there a way in Java to rectifythis problem???ThanksSameer

Answer:
To obtain the current screen size requires interfacing with the nativewindow system. The Java AWT library allows this through the Toolkitclass. The Toolkit class creates the bindings between the AWT classesand their native peers. You can obtain the default Toolkit by using theToolkit.getDefaultToolkit() static method, or you can directly obtainthe Toolkit used by a java.awt.Component derived class by calling itsgetToolkit() method.The Toolkit class has a method called getScreenSize() which returns areference to a Dimension instance that contains the screen width andheight. You can use this information to resize your frame to fill theentire screen or adjust its size to some percentage of the screendimensions.

The following short example prints the screen width and height tostandard output:

import java.awt.*;public final class ScreenSize {  public static final void main(String[] args) {    Dimension size;    size = Toolkit.getDefaultToolkit().getScreenSize();    System.out.println("Width: " + size.width + " Height: " + size.height);    System.exit(0);  }}
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist