[Resending; I used the wrong From address by accident...]

At 11:24 AM 10/24/2001 -0400, [EMAIL PROTECTED] wrote:

Some of the componets I'm arranging using GridBagLayout have
dynamically varying contents.  The problem is that sometimes, when the
contents of one of these components change, GBL resizes the
components!  (The sample code below illustrates the problem.  Clicking
on the button causes its text to change, which results in a change in
the sizes of the GUI's components.)  I want the resizing of components
to happen if and only if the user resizes application window.  How can
I configure GBL to enforce this constraint?

Okay, I think I see what you're going for here.  Starting with an interface
like this:

   | [list] [button] |

If the button's label changes from "button" to "1234567890", the interface
should then look like:

   | [list] 23456789 |

where the button's now too wide to fit.  And the list looks exactly the
same.  If the user then resizes, it can go to

   | [list] [1234567890] |

or

   | [list      ] [1234567890] |

or whatever, according to the new size.  (I'll assume here that on a
resize, you want the list to take up all extra space.)  Starting with the
first interface again, then, if the button label changes to "1234", it
should then look like:

   | [list]  [1234]  |

or

   | [list] [ 1234 ] |


That is, either the space the button's in, or the button itself, is
unchanged in width.  (Letting the button's width change to fit the label
would be most consistent with the "too long" behavior.)  Again, if/when the
user resizes the frame, the layout is redone appropriately.

The problem in Sick2 is that GBL is going to want to re-layout everything
because of the change in the button.  (You could put the button in its own
JScrollPane, and that would keep the GBL from noticing, I think, but you
wouldn't want that either.)

Usability-wise, you're not going to want a component to be clipped for lack
of space.  (I'm guessing that's why you asked earlier how to force a Window
to a certain minimum size.)  If a component's size is dynamically increasing
to where it no longer fits completely in its space, it should go in a
scroll pane if it's intended for use there (list, tree, table, panel in use
as a canvas, etc.).  If it's not intended to scroll, you're now in
UnorthodoxLand, where interfaces must be analyzed case-by-case.

In this case, you have a button whose label changes.  For this, I shall
claim that the size of a button, or the size of the space in the interface
where the button lives, should never change.

If you're in a quick-and-dirty mood, call setPreferredSize() on the button
and set it to accomodate the longest possible label it could have.  (If you
don't know the longest label beforehand, set the prefsize to something
reasonable, and note that if the label can't fit, the button will probably
display something like "very long label n...".  This is fine, since you can
get yourself into trouble anyway if you don't limit the button's maximum
size.)

If you want this button to be reusable, you'll need to reset the prefsize
in some of the other methods that change the button, such as setIcon(),
setMargin(), etc., or override getPreferredSize() to return whatever the
buttonUI would return for the longest label.

GBL wouldn't be touched.

_______________________________________________
Swing mailing list
[EMAIL PROTECTED]
http://eos.dk/mailman/listinfo/swing

Reply via email to