Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8305653

Window.setMinimumSize has problem if the scale of windows10 is higher than 100%

XMLWordPrintable

    • x86_64
    • windows_10

      ADDITIONAL SYSTEM INFORMATION :
      System&OS:
      Windows 10 Home, version 1903, build 18362.356

      Java:
      openjdk version "17.0.6" 2023-01-17 LTS
      OpenJDK Runtime Environment Corretto-17.0.6.10.1 (build 17.0.6+10-LTS)
      OpenJDK 64-Bit Server VM Corretto-17.0.6.10.1 (build 17.0.6+10-LTS, mixed mode, sharing)

      A DESCRIPTION OF THE PROBLEM :
      If the scale of windows10 is higher than 100%,the window size of the application can be shrunk to a lower size which is set by Window.setMinimumSize

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. Open the Display Settings of Windows10, Change the scale to 125%.
      2. Compile and run the test program below. When the window appears, attempt to shrink it by mouse.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The window size can't be shrunk, because the initial size is equals to the minimum size
      ACTUAL -
      The window size can be shrunk, it can be shrunk to 160*160, the result of 200/1.25.

      ---------- BEGIN SOURCE ----------
      public static void main(String[] args) {
          JFrame jFrame = new JFrame();
          jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

          jFrame.setLayout(new GridLayout(2,1));

          JLabel jLabel = new JLabel("This is a 200*200 box", JLabel.CENTER);
          jFrame.add(jLabel);

          JButton jButton = new JButton();
          jFrame.add(jButton);
          jButton.addActionListener(new ActionListener() {
              @Override
              public void actionPerformed(ActionEvent e) {
                  System.out.println(jFrame.getSize());
              }
          });

          jFrame.setMinimumSize(new Dimension(200,200));
          jFrame.pack();
          jFrame.setVisible(true);
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      If the scale of the windows10 is 100%, Window.setMinimumSize works correctly, the window size can't be shrunk lower than the size you set. If you change the scale higher than 125%, it has problem.

      There are two size in Swing, program size and screen size, the screen size will consider the scale of system, the program size is always the same in different scale which can be got by getSize() and be set by setSize().

      It seems the resize operation by mouse doesn't consider the scale setting of the System. When resize operation begin, I guess Swing get the screen size of window size and compare it to the program size of minimum size, but the screen size and the program size is not the same, it shouldn't be compared directly when the scale is higher than 100%.

      In the test code, program size of window is 200*200, screen size shounld plus 1.25, the result is 250*250. the minimum size is 200*200. it compares 250*250(window size) with 200*200(minimum size) so the window can shrunk. When compares, the minimum size shunld plus 1.25.

      I try to bypass this problem by recalculating the size when I use setMinimumSize, like this:
      ---------
      double scale; // get by system setting
      jFrame.setMinimumSize(new Dimension(200*scale,200*scale));
      ---------
      it works, but the setMinimumSize will compare the current window size to minimum size you set, if the size you set is higher than the window size, it will use setSize() to change the window size.In the test code, if use setMinimumSize(200*.125, 200*.125), the window size will change to 250*1.25, which is 312. It can make minimumSize works correctlly, but the window size become bigger.

      FREQUENCY : always


            pnarayanaswa Praveen Narayanaswamy
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: