AWT problem on linux

jonkv at jonkv at
Wed Sep 9 13:14:04 PDT 1998

gelderk at writes:

> Who says Component Z can keep the same size if I enlarge Component X
> in the same Container? I think that's the responsibility of the
> LayoutManager (what else are minimum and preferred sizes for?)

The components may not be able to keep the same size, but any
component that hasn't been invalidated can cache its preferred /
minimum / maximum size.  For example, if you invalidate() a button and
then validate() the Frame it is in, you don't want to have to
recalculate the preferred size for a List component with a thousand
items (a thousand strings whose widths need to be calculated...).  If
you always did an unconditional layout of the entire Frame, it seems
you would have to do a lot of unnecessary work.

> Apart from this, the whole validate() system might be circumvented by
> something like (called from add(), remove(), setBounds() or whatever,
> instead of invalidate()):
> public void doLayoutUpwards() {
>   doLayout();
>   if (parent!=null) parent.doLayoutUpwards();
> }

Suppose you have a dialog which can be expanded to show more
("advanced") options.  To show more options, you add a couple of dozen
new buttons, checkboxes, labels, and so on to the dialog.  Wouldn't
the doLayoutUpwards() solution (at least in the implementation above)
mean that doLayout() would be called at least once for every new item
you add?

Anyway, Swing seems to have solved this in a nice way.  Since Swing is
not thread-safe, you are supposed to perform all GUI updates from the
event dispatch thread.  The revalidate() method, which seems to be
called automatically by all methods that change a component (including
setFont(), setBorder(), setText(), and similar methods), first calls
invalidate() and then queues a request for validation (for the root of
the component hierarchy) as a new event on the event dispatch thread.

Since you are *in* the event dispatch thread, you can continue
changing/adding components.  Swing keeps track of the currently queued
requests, so it won't add any more validation requests for the same
root.  When you are done changing/adding components, the event
dispatch thread will eventually get to the validate() request and
validate() will be called.  The programmer never has to call
invalidate() or validate() manually.

More information about the kaffe mailing list