Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2006

Tip: Looking for answers? Try searching our database.

A fixed size window in Java

Thread view: 
C. - 29 Apr 2006 16:07 GMT
Hello. I am writing a Java application with Swing and I need a Window
with a fixed size, because every time I call "pack" because some
components of that window change, the Window is resized to fit exactly
that components.

If I use Frame.setPreferredSize(...) then the components are not shown
correctly when I call pack (some ones are displayed over other ones and
so on).

What can I do to implement a fixed size window?

Thank-you very much.
Thomas Hawtin - 29 Apr 2006 16:12 GMT
> Hello. I am writing a Java application with Swing and I need a Window
> with a fixed size, because every time I call "pack" because some
> components of that window change, the Window is resized to fit exactly
> that components.

If you use validate instead of pack, then the window will not be
resized. However, if the window is set to small, it'll be a mess.

> If I use Frame.setPreferredSize(...) then the components are not shown
> correctly when I call pack (some ones are displayed over other ones and
> so on).

Adding the contents of your window to a scroll pane is worth doing. Even
if the window is resized too small, it still wont look totally daft.

You might be better off setting a minimum size. Then if the contents are
too big for the size you have set, a sufficient size will be used. From
1.5, the minimum size of a frame will tend to be honoured when the user
resizes.

It can be useful to override a container's getXyzSize method. For
instance, a minimum size that is below the preferred size can cause a
mess. Or the preferred size might be silly small, and you want to add
some internal padding on.

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

C. - 29 Apr 2006 17:41 GMT
Thomas Hawtin escribió:
>> Hello. I am writing a Java application with Swing and I need a Window
>> with a fixed size, because every time I call "pack" because some
[quoted text clipped - 22 lines]
>
> Tom Hawtin

I have tried that but now I have the same problem. The objets are not
shown correctly. I have a Panel with some JLables layed out with
GridBagLayout. When some labels are deleted and I want to be redisplayed
that area using validate(), then the old labels are not removed (the
actual JLabel are removed, but their graphics are not).
C. - 29 Apr 2006 17:49 GMT
> I have tried that but now I have the same problem. The objets are not
> shown correctly. I have a Panel with some JLables layed out with
> GridBagLayout. When some labels are deleted and I want to be redisplayed
> that area using validate(), then the old labels are not removed (the
> actual JLabel are removed, but their graphics are not).

The problem is that the window is not repainted. I have minimized and
maximized it and the display area has been repainted ok. But calling
repaint() after validate() doesn't work.

How do you think I can fix that?

Thanks!!!
Thomas Hawtin - 29 Apr 2006 17:43 GMT
> The problem is that the window is not repainted. I have minimized and
> maximized it and the display area has been repainted ok. But calling
> repaint() after validate() doesn't work.

parent.revalidate(); parent.repaint(); should work. A big advantage of
revalidate over invalidate is that you can call it as much as you want,
but all the calls will be collected together so it wont be slow.

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

Stefan Ram - 29 Apr 2006 18:00 GMT
>What can I do to implement a fixed size window?

 Implement and use a custom layout manager?
James McGill - 29 Apr 2006 18:16 GMT
> Hello. I am writing a Java application with Swing and I need a Window
> with a fixed size, because every time I call "pack" because some
[quoted text clipped - 8 lines]
>
> Thank-you very much.

It might be worth your while to look into JLayeredPane.  Instead of a
LayoutManager, it lets you stack things on top of another with specific
Bounds relative to the container.  This gives you some control that
regular layout managers don't.  

You will still have the same problems if you pack() a container, but if
the LayeredPane's bounds and size getters return the right values, it
will do the right thing.

Lot of work, but any kind of custom layout always is, and LayeredPane is
not that bad...


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.