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 / March 2006

Tip: Looking for answers? Try searching our database.

JPopupMenu: Adding items when already visible

Thread view: 
spyromus - 09 Mar 2006 14:19 GMT
Hi,

I need to add new items to the popup menu when it's already visible. It
looks like Swing JPopupMenu doesn't like this and allows the code to
build menu layout only before showing it up. Any tricks or suggestions?

Thanks,

Aleksey
Bart Cremers - 09 Mar 2006 14:45 GMT
Don't know what really the problem is, as you do not state it, but did
you try to call pack() on the JPopupMenu after adding the new
menuitems?

Bart
spyromus - 09 Mar 2006 15:44 GMT
Magic! The problem was that once I had my popup menu visible, no matter
how hard I was trying to add items to the menu they refused to appear.
The solution with "pack()" after addition works fine, but isn't it a
little bit heavyweight?

Also it's not quite clear why they decided to model it this way. Any
thoughts?

Thanks!
Bart Cremers - 09 Mar 2006 18:04 GMT
It's of course pretty heavy, but I think people at Sun imagined that
normally menu items will not be added to a popupmenu (or even menubar
menu) while the menu is visible, which is of course correct for normal
GUI use.
And for these odd cases where you might need it you'll have to go
through this solution or provide you own menu implementation to
optimize stuff:

Bart
Thomas Hawtin - 09 Mar 2006 18:38 GMT
> It's of course pretty heavy, but I think people at Sun imagined that
> normally menu items will not be added to a popupmenu (or even menubar
> menu) while the menu is visible, which is of course correct for normal
> GUI use.

AWT and Swing containers in general do not revalidate on adding or
removing. Pop-ups don't seem to be any different.

I guess it's for historical reasons. AWT doesn't do anything like
revalidating (except repaint).

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

Aleksey Gureev - 09 Mar 2006 20:18 GMT
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
Vova Reznik - 09 Mar 2006 20:54 GMT
> 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


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.