• Post Reply Bookmark Topic Watch Topic
  • New Topic

JWindow size

 
Andy Powell
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone, I have a class that extends JWindow. It contains a JPanel with a cardlayout that contains 2 cards at the moment.

I am setting the size with


However, the JWindow always appears smaller than its content and never at the size specified above.

The complete code is as follow, I know its a bit messy at the moment but I haven't played with this stuff in a long time...


As you can see, I've tried setBounds on everything to try and make it work.

Any assistance would be greatly appreciated.

Thanks
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try setSize() and setLocationRelativeTo()
 
Rob Spoor
Sheriff
Posts: 20817
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Calling setBounds on the JPanel will not work as the window's layout manager (BorderLayout by default, if I recall correctly) will override it. The call to pack() will resize the window to its preferred size which is in this case the preferred size of the JPanel. Call setBounds on the window instead of the panel, after the call to pack(). A combination of setSize and setLocationRelativeTo will do basically the same; if you pass null to the latter the window will be centered on the screen.

One question though: you use Double and its intValue() method. Why not use double and cast it to int:
This will do exactly the same (as Double.intValue() also simply casts the double value to int) but saves the creation of the Double objects.
 
Andy Powell
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob! That worked a treat. Can you explain what exactly pack() does? I'm slightly confused.

I tried casting the doubles to ints but it kept complaining that I couldn't but it works perfectly now the way that you have shown.
I must have been doing it wrong.

Thank you very much
 
Darryl Burke
Bartender
Posts: 5152
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, for this particular case there's no need to cast at all. Dimension has public int fields width and height. Use those instead of the getWidth() / getHeight() methods (which cast the field values to double and return the field as a double, which you again cast to int ...)

 
Rob Spoor
Sheriff
Posts: 20817
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know, but I was worried about truncating while dividing. However, the .5 or .25 that the double division would give would be truncated by the cast to int, so it doesn't matter if you perform double division and then truncate, or perform integer division. The result is going to be the same anyway.
 
Darryl Burke
Bartender
Posts: 5152
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
... and in the final analysis, a window can only be located at a pixel boundary, so any fractional portion has to be discarded anyway.
 
Andy Powell
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks very much for all your help. Its perfect.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!