Search code examples
javaswingresolutiondpijava-9

Set resolution manually in Java Swing


I need a way to manually control/stretch the size of my whole GUI.

I have a 1400 x 1050 Desktop (native resolution) and I want to scale the resolution manually to 1024 x 1050 inside the code, because my application was written on/for a 1024 x 768 Desktop. So in general the whole frame and all buttons etc should be stretched / bigger as a whole and the relation should stay the same.

I can´t do it via Windows properties because the resolution in general needs to be 1400 x 1050 because of another application.

My approach was something like:

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
screenHeight = screenSize.height;
screenWidth = screenSize.width; 

..and then change the screen size via setSize()? But I don´t understand how.

Java Tookit Screen Resolution - How can I use it?

..unfortunately the link in the answer here does not work anymore.

How to set resolution manually in Java Swing?


Solution

  • As mentioned in the comments, the best approach here is to fix the underlying code to not mandate a 1024 x 768 display. This is usually a very distinct code smell of front-end Java code. Proper use of Layout Managers can almost always get your display to function in a more flexible manner.

    However, in industry, sometimes refactoring to get legacy components functioning properly is not a feasible effort. In such a case, I would propose that you treat the inflexible GUI you can't refactor as a component of a larger GUI that you can control.

    Consider the following example code:

    public static void main(String[] args) {
      // Initialize the frame
      JFrame myApp = new JFrame("App");
      myApp.setSize(1400, 1050);
    
      // Create container for the GUI
      JPanel container = new JPanel(new BorderLayout());
      container.setPreferredSize(new Dimension(1024, 768));
    
      // Load the GUI into the container
      JComponent myGui = new JPanel(); // Replace this with actual GUI.
      myGui.setBackground(Color.RED); // Remove this once using actual GUI.
      container.add(myGui, BorderLayout.CENTER);
    
      // Create the frame's content pane
      JPanel content = new JPanel(new FlowLayout(FlowLayout.CENTER));
      content.setBackground(Color.BLUE); // Also remove this in production.
    
      // Add GUI to content pane
      content.add(container);
    
      // Add content pane to frame, show frame
      myApp.setContentPane(content);
      myApp.setVisible(true);
    }
    

    This works because we've added the GUI to the CENTER of a BorderLayout panel (which will stretch the GUI to occupy the entire size of the panel in the absence of any components on the NORTH/SOUTH/EAST/WEST). We set the preferred size of that BorderLayout panel to be 1024 x 768 (IE: the size that the GUI is specified to work for), and then feed that panel into a FlowLayout panel (which will preserve the preferred size).

    The result is that our 1400 x 1050 application contains a 1024 x 768 rendering of our GUI component (you can easily change this to 1024 x 1050 by modifying the preferred size of the panel containing the BorderLayout).

    As an exercise to the user, you'll notice that the GUI code isn't centered vertically if you run this. This can be tackled by modifying the layout of the content panel.