Search code examples
javaswingjava-web-start

Java Web Start JFrame size not respected


I'm creating a new JFrame from inside a Java Web Start application. The content of the frame often changes and it wasn't resizing, so I overrode revalidate to explicitly set the frame size. When I run the application in my IDE (Idea), the size of the frame is what I expect based on the preferredSize and minimumSize. When I bundle the application into a WebStart app, deploy it, and run it through javaws, the window is much much larger than I expect.

I've created the test case below:

public static void main( String args[] ) {

  JFrame sampleFrame = new JFrame() {
    public void revalidate() {
      super.revalidate();
      pack();
      _log.severe( "BEFORE: Preferred Size = " + getPreferredSize() + " - Minimum Size = " + getMinimumSize() + " - Size = " + getSize() );
      setSize( getPreferredSize() );
      _log.severe( "AFTER: Preferred Size = " + getPreferredSize() + " - Minimum Size = " + getMinimumSize() + " - Size = " + getSize() );
    }
  };

  sampleFrame.getContentPane().setPreferredSize( new Dimension( 100, 100 ) );
  sampleFrame.pack();
  sampleFrame.setVisible( true );
  sampleFrame.revalidate();
}

When I run the test through WebStart, the console indicates:

SEVERE: BEFORE: Preferred Size = java.awt.Dimension[width=110,height=130] - Minimum Size = java.awt.Dimension[width=10,height=30] - Size = java.awt.Dimension[width=110,height=130] SEVERE: AFTER: Preferred Size = java.awt.Dimension[width=110,height=130] - Minimum Size = java.awt.Dimension[width=10,height=30] - Size = java.awt.Dimension[width=110,height=130]

Even though the log says that the JFrame size is 110x130, it is actually around 1300x930! Any ideas why that might be?

I'm running Debian 6.0.7 ("squeeze") and Java(TM) Web Start 10.45.2.18-fcs with (Java build 1.7.0_45-b18)


Solution

  • Try JFrame.setBounds() instead of setSize() -- it's what I've always used to resize JFrames, though I cannot tell from the javadocs just what the difference is between the two.