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

JFrame width will not exceed monitor size on startup

XMLWordPrintable

      ADDITIONAL SYSTEM INFORMATION :
      Centos 7.5
      3.10.0-862.14.4.el7.x86_64 #1 SMP Wed Sep 26 15:12:11 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux

      Java(TM) SE Runtime Environment 18.9 (build 11.0.1+13-LTS)
      Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.1+13-LTS, mixed mode)


      A DESCRIPTION OF THE PROBLEM :
      My JFrame wraps components whose total width exceeds the 1920 pixel width of my display. When my program launchs, I see that it sizes my JFrame to fit the monitor width. I was able to duplicate this problem using the FrameDemo .java program (https://docs.oracle.com/javase/tutorial/displayCode.html?code=https://docs.oracle.com/javase/tutorial/uiswing/examples/components/FrameDemoProject/src/components/FrameDemo.java).

       I modified it slightly to make the width of the emptyLabel object equal to 3000 pixels. Here is the exact code:

      import java.awt.*;
      import java.awt.event.*;
      import javax.swing.*;

      /* FrameDemo.java requires no other files. */
      public class FrameDemo {
          /**
           * Create the GUI and show it. For thread safety,
           * this method should be invoked from the
           * event-dispatching thread.
           */
          private static void createAndShowGUI() {
              //Create and set up the window.
              JFrame frame = new JFrame("FrameDemo");
              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

              JLabel emptyLabel = new JLabel("blahblah");
              emptyLabel.setPreferredSize(new Dimension(3000, 100));
              frame.getContentPane().add(emptyLabel, BorderLayout.CENTER);

              //Display the window.
              frame.pack();
              frame.setVisible(true);
          }

          public static void main(String[] args) {
              //Schedule a job for the event-dispatching thread:
              //creating and showing this application's GUI.
              javax.swing.SwingUtilities.invokeLater(new Runnable() {
                  public void run() {
                      createAndShowGUI();
                  }
              });
          }
      }

      When I run this on my dual monitor setup, the JFrame spans only one of my monitors. I can make it work by replacing:
              frame.pack();
              frame.setVisible(true);
      with:
             Dimension orig = frame.getMinimumSize();
      frame.setMinimumSize(new Dimension(3000, 100));
              frame.pack();
              frame.setVisible(true);

      This is a dirty trick that I don't expect to have to play. I posted this on StackExchange and a programmer there pointed out that it's the call to setVisible that is resetting the preferred size to fit the screen width.

      Strangely enough, with this little demo, if I add frame.setMinimumSize(orig); after frame setVisible(true); the frame again fails to span my two screens.


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Copy paste the first code block into a file, compile and run. It fails all the time on my machine. It works with the workaround I've suggested.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      I expect my frame to be wider than a single monitor.
      ACTUAL -
      The frame spans only one monitor in spite of the width of one of it's components exceeding the monitor width.

      ---------- BEGIN SOURCE ----------
      import java.awt.*;
      import java.awt.event.*;
      import javax.swing.*;

      /* FrameDemo.java requires no other files. */
      public class FrameDemo {
          /**
           * Create the GUI and show it. For thread safety,
           * this method should be invoked from the
           * event-dispatching thread.
           */
          private static void createAndShowGUI() {
              //Create and set up the window.
              JFrame frame = new JFrame("FrameDemo");
              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

              JLabel emptyLabel = new JLabel("blahblah");
              emptyLabel.setPreferredSize(new Dimension(3000, 100));
              frame.getContentPane().add(emptyLabel, BorderLayout.CENTER);

              //Display the window.
              frame.pack();
              frame.setVisible(true);
          }

          public static void main(String[] args) {
              //Schedule a job for the event-dispatching thread:
              //creating and showing this application's GUI.
              javax.swing.SwingUtilities.invokeLater(new Runnable() {
                  public void run() {
                      createAndShowGUI();
                  }
              });
          }
      }

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

      CUSTOMER SUBMITTED WORKAROUND :
      I can make this program work by replacing:
              frame.pack();
              frame.setVisible(true);
      with:
             Dimension orig = frame.getMinimumSize();
      frame.setMinimumSize(new Dimension(3000, 100));
              frame.pack();
              frame.setVisible(true);

      This is a dirty trick that I don't expect to have to play. I posted this on StackExchange and a programmer there pointed out that it's the call to setVisible that is resetting the preferred size to fit the screen width.

      Strangely enough, with this little example, if I add frame.setMinimumSize(orig); after frame setVisible(true); the frame again fails to span my two screens.


      FREQUENCY : always


            Unassigned Unassigned
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated: