Sounds pretty reasonable. Although, I wouldn't be so sure about general
rules for Swing containers. Basically, my experience shows that if I
add something to the visible panel, it appears and exactly the fact
that the menu behaved somehow differently was a bit confusing.
Anyway, I agree that it's not a normal situation when a menu is being
updated "on the fly", or at least not from the conventional point of
view. :) Good to see that some quick workaround is there or I would end
up writing my own menu-like control.
Thanks guys! You were extremely helpful.
Aleksey
> Sounds pretty reasonable. Although, I wouldn't be so sure about general
> rules for Swing containers. Basically, my experience shows that if I
> add something to the visible panel, it appears and exactly the fact
> that the menu behaved somehow differently was a bit confusing.
You may want to read javadoc for
JComponent public void revalidate()
> Anyway, I agree that it's not a normal situation when a menu is being
> updated "on the fly", or at least not from the conventional point of
[quoted text clipped - 4 lines]
>
> Aleksey
Bart Cremers - 09 Mar 2006 22:15 GMT
revalidate(), invalidate() and even doLayout() will not do what is
needed here. The menu will not resize in these cases and then try to
paint the all menu items in the available space. This gives a funny
result.
Only pack() (AFAIK) will give the required result.
Bart
Vova Reznik - 09 Mar 2006 22:31 GMT
> revalidate(), invalidate() and even doLayout() will not do what is
> needed here. The menu will not resize in these cases and then try to
[quoted text clipped - 3 lines]
>
> Bart
I haven't try it, but code (for JPopupMenu) says that pack() calls validate.
That is why I believe revalidate() will do the job too.
[doc for JPopupMenu revalidate()]
This method will automatically be called on this component
* when a property value changes such that size, location, or
* internal layout of this component has been affected.
This automatic updating differs from the AWT because programs generally
no longer need to invoke <code>validate</code> to get the contents of
the GUI to update.
[/doc]
j2sdk1.4.2_04 (JPopupMenu)
[code]
public void pack() {
if(popup != null) {
Dimension pref = getPreferredSize();
if (pref == null || pref.width != getWidth() ||
pref.height != getHeight()) {
popup = getPopup();
} else {
validate();
}
}
}
[/code]
Bart Cremers - 09 Mar 2006 22:36 GMT
Well, believe me, I tried it. Validate will do a repaint of the menu,
showing the added items, but it forgets about the resizing step. This
gives a really funny result.
Bart