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

Expanding JFrame to full screen before calling setVisible fails in macOS X

XMLWordPrintable

      ADDITIONAL SYSTEM INFORMATION :
      OS: macOS Mojave version 10.14
      Java: 8 Update 231
      Computer: iMac Retina 5K, 27 inch, Late 2015

      I recently upgraded from a previous version of MacOS (perhaps Yosemite or El Capitan); the problem did not appear before the upgrade. There is no problem on Windows 10.

      A DESCRIPTION OF THE PROBLEM :
      If setSize is called on the main application JFrame of a Swing application before calling setVisible(true), and the target size is the full screen (minus menu bar and Dock), the JFrame will shrink to show only the three buttons in the upper left corner, and move to the lower left corner of the screen. If the target size is 11 or more pixels narrower than the main window, or setSize is called after setVisible, the window expands correctly.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      I have provided source code below for a test case; simply run the code. There are two flags that can be changed to either demonstrate the bug, or demonstrate a workaround

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The application window should expand to full screen
      ACTUAL -
      The application window shrinks to about 23x50 pixels, and moves to the lower left corner of the screen.

      ---------- BEGIN SOURCE ----------
      import java.awt.Color;
      import java.awt.Dimension;
      import java.awt.GraphicsConfiguration;
      import java.awt.GraphicsDevice;
      import java.awt.GraphicsEnvironment;
      import java.awt.Insets;
      import java.awt.Toolkit;
      import java.awt.event.ActionEvent;
      import java.awt.event.ActionListener;

      import javax.swing.JButton;
      import javax.swing.JFrame;
      import javax.swing.JPanel;

      public class WindowSizeTest extends JFrame implements ActionListener {
      final JButton m_quitButton = new JButton("Quit");

      /**
      * The window will resize to fill the screen if bResizeAfter is true or
      * bAddMargin of error is >10. Otherwise, it will shrink and move to the
      * lower left corner.
      */
      final boolean bResizeAfter = false;
      final int marginOfError = 0;

      protected WindowSizeTest() {
      super("WindowSizeTest");
      m_quitButton.addActionListener(this);
      final JPanel panel = new JPanel();
      panel.add(m_quitButton);
      setContentPane(panel);
      pack();

      if (!bResizeAfter) {
      makeFullScreen();
      }

      setVisible(true);

      if (bResizeAfter) {
      makeFullScreen();
      }
      System.out.println("ctor: returned from setVisible");
      }

      public static void main(String[] args) {
      new WindowSizeTest();
      }

      private void makeFullScreen() {
      final Toolkit tk = Toolkit.getDefaultToolkit();
      final Dimension dim = tk.getScreenSize();
      final Insets insets = tk.getScreenInsets(getDefaultGraphicsConfiguration());

      // Set the frame to fit the default screen
      int width = dim.width - insets.left - insets.right;
      final int height = dim.height - insets.top - insets.bottom;

      width -= marginOfError;

      setSize(width, height);
      System.out.println(String.format("makeFullScreen: width=%d height=%d", width, height));
      setBackground(Color.white);

      }

      private static GraphicsConfiguration getDefaultGraphicsConfiguration() {
      GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
      GraphicsDevice[] gs = ge.getScreenDevices();
      GraphicsDevice gd = gs[0];
      GraphicsConfiguration[] gc = gd.getConfigurations();

      return gc[0];
      }

      @Override
      public void actionPerformed(ActionEvent e) {
      if (e.getSource() == m_quitButton) {
      System.exit(0);
      }
      }

      @Override
      public void reshape(int x, int y, int width, int height) {
      super.reshape(x, y, width, height);
      System.out.println(String.format("reshape(%d,%d,%d,%d)", x, y, width, height));
      }

      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Call setSize after setVisible, or call setSize with a width 11 or more pixels narrower than the screen.

      FREQUENCY : always


            bvaidya Balchandra Vaidya
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: