• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JWindow size

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try setSize() and setLocationRelativeTo()
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks very much for all your help. Its perfect.
 
Montana has cold dark nights. Perfect for the heat from incandescent light. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic