GeeCON Prague 2014*
The moose likes Swing / AWT / SWT and the fly likes JFrame.setSize() calls ignored until after JFrame.show() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JFrame.setSize() calls ignored until after JFrame.show()" Watch "JFrame.setSize() calls ignored until after JFrame.show()" New topic
Author

JFrame.setSize() calls ignored until after JFrame.show()

Jan Ballen
Greenhorn

Joined: Feb 08, 2005
Posts: 3
Does anyone know why JFrame.setSize() calls are ignored until after show() is called. It seems that when calling JFrame.setSize() before a JFrame.show() call, the sizes are updated, but upon calling JFrame.show() the sizes are reset to another value, which happends to be the size of the last JFrame closed. The same happens with setLocation().

To see what I mean run the following code, and you'll see what I mean:

public class TestFrame extends JFrame
{

private JFrame mFrame;

/**
*
*/
public TestFrame()
{
super("800x1600");
mFrame = new JFrame("100x200");
System.out.println("constructor a: " + mFrame.getSize());
mFrame.setSize(new Dimension(100,200));
mFrame.validate();
System.out.println("constructor b: " + mFrame.getSize());
mFrame.show();
System.out.println("constructor c: " + mFrame.getSize());

JButton button = new JButton(new AbstractAction() {

public void actionPerformed(ActionEvent e)
{
JFrame frame = new JFrame("200x400");
System.out.println("action a: " + frame.getSize());
frame.setSize(new Dimension(200,400));
System.out.println("action b: " + frame.getSize());
frame.show();
System.out.println("action c: " + frame.getSize());
}});
this.getContentPane().add(button);
}

public static void main(String[] args)
{
TestFrame frame = new TestFrame();

System.out.println("main a: " + frame.getSize());
frame.setSize(new Dimension(800,1600));
System.out.println("main b: " + frame.getSize());
frame.show();
System.out.println("main c: " + frame.getSize());
}
}
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Your test code is way to complicated/confusing to even try and determine what the heck is going on. What are you trying to achieve exactly? The way you have your code, you are creating an instance of TestFrame which extends JFrame but then in the constructor you are creating a new JFrame all together (mFrame) and then when you click a button, you open another frame. Not to mention the show() method has been depricated since JDK1.3 Let's try some different code, shall we?



GenRocket - Experts at Building Test Data
Jan Ballen
Greenhorn

Joined: Feb 08, 2005
Posts: 3
Thanks for looking into this.

The JFrame show method is not depreciated, at least according to my 1.4.2 JDK source. It shows that JFrame inherits it's show method from the Window class. JComponent's show method is however depreciated, which is probably what you were thinking of. In fact I originally thought this may have been the problem, which is why I specifically looked into the difference between the show and setVisible methods.

The code may have been complicated, but that is in order to illustrate the problem fully. Did you run it?

But your code example will also illustrates some of the problem. If you run your code, , (fix the add first to add to the ContentPane though), you will notice that when you run your code it does not set the Jrame size correctly when setting it for the first time, in the constructor. In fact it remembers the size from any previous TestFrame. Even in between different program executions! To see what I mean, run your code, then resize the frame manually, and close the frame. Then run the code again. You'll see that the frame size is ignored the first time, and the frame size (the one you manually resized) from your last program execution is used.

Now with my code example, you'll see that the TestFrame seems to reuse it's last window size, and the other JFrames, being just plain JFrames, share the same last size. The getSize print output shows that after calling show something resizes the Jframes.

This is when running on Windows XP, with j2sdk1.4.2_06.

Let me know if you get the same behaviour.

Cheers,
Jan
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

The JFrame show method is not depreciated, at least according to my 1.4.2 JDK source. It shows that JFrame inherits it's show method from the Window class. JComponent's show method is however depreciated, which is probably what you were thinking of. In fact I originally thought this may have been the problem, which is why I specifically looked into the difference between the show and setVisible methods.

I keep forgetting I am running JDK 1.5 in which the show method is pretty much depricated throughout all the components, not just JComponent. My fault.

(fix the add first to add to the ContentPane though)

Yet again, another JDK 1.5 difference.

But your code example will also illustrates some of the problem. If you run your code, , (fix the add first to add to the ContentPane though), you will notice that when you run your code it does not set the Jrame size correctly when setting it for the first time, in the constructor. In fact it remembers the size from any previous TestFrame. Even in between different program executions! To see what I mean, run your code, then resize the frame manually, and close the frame. Then run the code again. You'll see that the frame size is ignored the first time, and the frame size (the one you manually resized) from your last program execution is used.

Ok, here is what happens when I run my code. Keep in mind also that I get the same behavior on JDK 1.4. I run my code the first time and the window opens at 800x600. If I click the button, the frame is resized down to 300x200. If I click the button again, it goes back to 800x600. All is good there.

Now, if I run my program, manually drag the window to a larger or smaller size, close the frame, then run the program again, the frame opens at 800x600 which is what is expected as this is what I initially set my frames size at in the constructor.

In another test I did, instead of resizing the frame when the button is pressed, I created a new frame at 300x200. I clicked the button, a new frame opened. I then manually resized that frame and closed it. When I click the button again, another frame opens at 300x200 which is expected.

Then, I ran your code again. What I did notice that was odd is not really what you described but when you set the frame size and print out it's dimensions, the size prints correctly. However, after the frame is realized (the show method is called) the width on the smaller window is different and the height on the TestFrame is different. It's not so much that it is remembering a specified size that you manually resized it to, because no matter what I do, I get the same initial heights and widths everytime I run it.

constructor a: java.awt.Dimension[width=0,height=0]
constructor b: java.awt.Dimension[width=100,height=200]
constructor c: java.awt.Dimension[width=123,height=200]
main a: java.awt.Dimension[width=0,height=0]
main b: java.awt.Dimension[width=800,height=1600]
main c: java.awt.Dimension[width=800,height=1036]

So I did some more testing with mine and if I set the height of my frame anything higher than 1036, it always goes to 1036. Anything lower and it will size to that size. I'm not sure if this is a bug or what but I definatly want to look into it and find out what the heck is going on.
Jan Ballen
Greenhorn

Joined: Feb 08, 2005
Posts: 3
Ah ok, good to hear 1.5 makes things clearer on depreciating show everywhere.

But I have found out why it remembered sizing of windows. It took me a while to figure this out. The 2 pcs I tried this on are both dual monitor and run ATI's Hydravision dual monitor software, which has options to remember the screen size and location of an application. :roll:

However when I switched that off, I now also see similar behaviour to you, but slightly different. My output is:

constructor a: java.awt.Dimension[width=0,height=0]
constructor b: java.awt.Dimension[width=100,height=200]
constructor c: java.awt.Dimension[width=140,height=200]
main a: java.awt.Dimension[width=0,height=0]
main b: java.awt.Dimension[width=800,height=1600]
main c: java.awt.Dimension[width=800,height=1036]

But that is explained by the fact that Windows wont let you make the width of a Window less than a certains size, and it limits the total height as well.

Thanks for your help, I finally can sleep at night
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Originally posted by Jan Ballen:
Ah ok, good to hear 1.5 makes things clearer on depreciating show everywhere.

But I have found out why it remembered sizing of windows. It took me a while to figure this out. The 2 pcs I tried this on are both dual monitor and run ATI's Hydravision dual monitor software, which has options to remember the screen size and location of an application. :roll:

However when I switched that off, I now also see similar behaviour to you, but slightly different. My output is:

constructor a: java.awt.Dimension[width=0,height=0]
constructor b: java.awt.Dimension[width=100,height=200]
constructor c: java.awt.Dimension[width=140,height=200]
main a: java.awt.Dimension[width=0,height=0]
main b: java.awt.Dimension[width=800,height=1600]
main c: java.awt.Dimension[width=800,height=1036]

But that is explained by the fact that Windows wont let you make the width of a Window less than a certains size, and it limits the total height as well.

Thanks for your help, I finally can sleep at night


Whew! Glad to hear it is not some Java bug. Good job!
 
GeeCON Prague 2014
 
subject: JFrame.setSize() calls ignored until after JFrame.show()