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 / GUI / January 2006

Tip: Looking for answers? Try searching our database.

FlowLayout with maximum right margin

Thread view: 
Marc Dzaebel - 25 Dec 2005 12:56 GMT
setMaximumSize() is not recognized by FlowLayout. However, I don't want to
setSize(...) anything but only to pack() my JFrame.

Do I have to implement my own LayoutManager or use an other one for this
trivial task? Is there a general solution to the problem that one only wants
to set one of maximum width/height and the other one is computed?

Thanks, Marc
Andrew Thompson - 26 Dec 2005 02:50 GMT
> setMaximumSize() is not recognized by FlowLayout. However, I don't want to
> setSize(...) anything but only to pack() my JFrame.

You should not have to call setSize() on most GUI's.
Simply pack() and let the GUI find it's own size.

Signature

Andrew Thompson
physci, javasaver, 1point1c, lensescapes - athompson.info/andrew

Marc Dzaebel - 26 Dec 2005 09:10 GMT
>> setMaximumSize() is not recognized by FlowLayout. However, I don't want
>> to setSize(...) anything but only to pack() my JFrame.
>
> You should not have to call setSize() on most GUI's.
> Simply pack() and let the GUI find it's own size.

Thanks Andrew!

Yes, I just want to pack() but I need a right margin for the components in a
FlowLayout so that they wrap after this maximum width. This can be done by
setting the size of the Container. However, the height of the Container
should be adapted. I now managed to achieve this by getting the location of
the last component, reseting the height and repacking the frame. This method
can be used for several standard LayoutManagers that does not recognize
set[Minimum/Maximum]Size.

Thanks, Marc
ricky.clarkson@gmail.com - 26 Dec 2005 17:02 GMT
I would implement my own layout manager for this, but a short answer
will suffice:

1.

I think frame.pack() is roughly equivalent to
frame.setSize(frame.getPreferredSize()), so:

Dimension preferredSize=frame.getPreferredSize(); //this is what pack()
will resize to, I believe.

preferredSize=new
Dimension(max(preferredSize.getWidth(),600),preferredSize.getHeight());

600 would be the size that you like, and the max makes sure that if you
change the FlowLayout to something else and forget to change this code,
then you'll still get sane results.  max(int,int) would be a method
that's part of this class, or one of the static imports.

frame.setSize(preferredSize());

Note that I haven't tested this at all.
Marc Dzaebel - 27 Dec 2005 10:57 GMT
<ricky.clarkson@gmail.com>
>I would implement my own layout manager for this ...

Yes, that might be true, but then the standard LayoutManagers doesn't fit
into common requirements.

> I think frame.pack() is roughly equivalent to
> frame.setSize(frame.getPreferredSize()), so:

Correct.

> Dimension preferredSize=frame.getPreferredSize(); //this is what pack()
> will resize to, I believe.

The principal idea (using preferredSize) is right and you are the first who
really understand the problem! However, I have to setSize(...) before
getting the preferred size in oder to determine the right margin. In your
solution the computed height fits to a default width rather than my own
right margin. Here is my solution for a JPanel in a JFrame:

...
frame.getContentPane().add(panel); // panel with FlowLayout
panel.setPreferredSize(new Dimension(rightMargin,Integer.MAX_VALUE));
frame.pack();
if(panel.getComponentCount()!=0) {
 Component last=panel.getComponent(panel.getComponentCount()-1);
 int
bottom=last.getY()+last.getHeight()+((FlowLayout)panel.getLayout()).getVgap();
 panel.setPreferredSize(new Dimension(rightMargin,bottom));
 frame.pack();
}
frame.setVisible(true);

Anyway, as this is FlowLayout-specific it might be better to extend
FlowLayout. So the answer currently is: Standard LayoutManager's doesn't
even fit for standard cases.

Marc
ricky.clarkson@gmail.com - 29 Dec 2005 13:25 GMT
Marc,

> Standard LayoutManager's doesn't
> even fit for standard cases.

BoxLayout and GridBagLayout are both very flexible, and can probably
both handle whatever these requirements are.  I actually fail to know
where you want to get the width from, but that's due to lack of
concentration, or lack of clarity, or both.

You might be trying to do something that's outside what a LayoutManager
is supposed to do.

The LayoutManager2 interface (that 2 is not a typo) is pretty good, and
makes writing your own layout manager fairly easy.  I wrote
PercentLayout with it, no problem:

http://cime.net/~ricky/netsim/oldham/PercentLayout.java
and
http://cime.net/~ricky/netsim/oldham/Constraint.java
Marc Dzaebel - 01 Jan 2006 09:18 GMT
>> Standard LayoutManager's doesn't
>> even fit for standard cases.
>
> BoxLayout and GridBagLayout are both very flexible, and can probably
> both handle whatever these requirements are.  ...

Sure, they are indeed quite powerful but fail to wrap components as
FlowLayout does (to my knowledge). So still a right margin for wraping
components can't be easily done with a standard Layout.

> You might be trying to do something that's outside what a LayoutManager
> is supposed to do.
[quoted text clipped - 3 lines]
> http://cime.net/~ricky/netsim/oldham/PercentLayout.java
> http://cime.net/~ricky/netsim/oldham/Constraint.java

Thanks for this helpful links. I really have to engage more in
LayoutManager's!

Marc


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.