How to change display mode with Java Swings

Java 8Object Oriented ProgrammingProgramming

To change display mode with Java Swings, use the setDisplayMode() method. Here, we have set the Display mode as:

new DisplayMode(800, 600, 32, 60));

Now, when you will run the program, the frame would be visible in a different resolution than the actual set resolution of your system.

The following is an example to change display mode with Java Swings:


import java.awt.DisplayMode;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import javax.swing.JFrame;
public class SwingDemo {
   public static void main(String[] args) {
      JFrame frame = new JFrame();
      frame.setSize(800, 600);
      GraphicsDevice graphics = GraphicsEnvironment.getLocalGraphicsEnvironment()
      graphics.setDisplayMode(new DisplayMode(800, 600, 32, 60));

The output is as follows displaying a new display mode. Current mode of the system is 1366x768 and we have set 800x600 for the frame:

Published on 03-May-2019 07:25:49