<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