September 28, 2008

Published September 28, 2008 by

Fullscreen Frame in Java

We may have several frames or windows of various sizes in our Java programs. Sometimes we need fullscreen frame or window. Sometimes we do it by setting the width and height of the frame as same as the screen i.e., monitor like 800 x 640 or 1024 x 768 etc. But what will happen if the monitor is changed or the resolution of the monitor is changed or the program is run on different PCs with different monitor settings?

The solution is to set the frame’s width and height of the screen of the monitor dynamically according to the monitor settings. We can do this simply by writing the following code.

Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
setSize(dimension);


A complete code is given below to make a full screen frame.

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

public class FullscreenFrame extends JFrame {
    public FullscreenFrame() {
        Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
        setSize(dimension);
        setTitle("A Fullscreen Frame");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public static void main(String args[]) {
        FullscreenFrame fullscreenFrame = new FullscreenFrame();
        fullscreenFrame.setVisible(true);
    }
}