<Swing Dev> Bug 6499857: JMenuItem.getRootPane() returns null

Alexander Potochkin Alexander.Potochkin at Sun.COM
Thu Sep 20 18:52:57 UTC 2007

Hello Miguel

> The problem is simple. The getRootPane() method calls 
> SwingUtilities.getRootPane(Component c). That method loops on a call to 
> getParent, like this:
>         for( ; c != null; c = c.getParent()) {
>             if (c instanceof JRootPane) {
>                 return (JRootPane)c;
>             }
>         }
> The trouble is that anything placed on a menu doesn't belong to the 
> standard hierarchy. JMenus use a JPopupMenu, and JPopupMenus don't have 
> a parent, they have an invoker.
> An obvious fix would be to override getParent() in JPopupMenu to call 
> getInvoker() and return that. This doesn't work. The getInvoker() method 
> returns a Component, but getParent() returns a Container. I can work 
> around this, but it breaks the menu code.
> The fix is to complicate the loop on c = c.getParent(), by checking for 
> JMenuItems and handling them differently. This slows the loop down, so I 
> don't want to do that in Swing Utilities, because there's no need for 
> the more complicated loop for most components. Instead, I plan to 
> override getRootPane() in JMenuItem.

This sounds like a good idea

>   This is complicated by the fact that not all JMenuItems are broken. 
> JMenus are subclasses of JMenuItem, and they have a valid parent (unless 
> they're a submenu). But that should be easy to work around.
> I'll have some code in a day or two. I'll include a test program. Are 
> there any constraints I should know about for the test program?

We use jtreg harness for our regression tests


It might look a bit messy,
but actually it's quite simple to write a test with jtreg

In your case, I imagine, you'd check JMenuItem.getRootPane()
and throw RuntimeException if it is null just like with JUnit


> -- Miguel Muñoz
> On Sep 20, 2007, at 6:55 AM, Alexander Potochkin wrote:
>> Hello Miguel
>>> I plan to fix bug 6499857. There are a couple of approaches, but I 
>>> think I know the best way to fix it. Has anybody else looked at this?
>> Currently no one works on this fix
>> so you are welcome to start with it !
>> Thanks
>> alexp
>>> -- Miguel Muñoz
>>> ___________________
>>> There are 10 kinds of people: Those who know binary and those who don't.
> ___________________
> There are 10 kinds of people: Those who know binary and those who don't.

More information about the swing-dev mailing list