Aleksandr Kravets
Aleksandr Kravets

Reputation: 5947

Apple's JAVA: JMenu's submenu is not being updated

I've run into some bugs in Apple's JVM's as i mentioned in my previous question.And i can live with first bug. But second is really annoying. If i create a JMenu with submenu in it and i have to modify submenu contents in runtime, i just can't do it. Debugging shows that items are added to Jmenu (submenu). But nothing's happening in screen menubar. This looks like a problem of synchronization of real JMenu object and it's representation in Mac OS X menubar.

Here's sample code:

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.SwingUtilities;


public class TestMenu extends JFrame{

    public TestMenu() {
        System.setProperty("apple.laf.useScreenMenuBar", "true");
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        JMenuBar mb = new JMenuBar();
        mb.setName("menubar");
        JMenu menu = new JMenu("menu"); 
        JMenu submenu = new JMenu("submenu");
        JMenuItem item = new JMenuItem("test item");
        JMenuItem item2 = new JMenuItem("test item2");
        JMenuItem subitem1 = new JMenuItem("sub item1");
        JMenuItem subitem2 = new JMenuItem("sub item2");
        menu.add(item);
        mb.add(menu);
        menu.add(submenu);
        setJMenuBar(mb);
        menu.add(item2);
        setBounds(100, 100, 100, 100);
        setVisible(true);
        submenu.add(subitem1);
        submenu.add(subitem2);
    }

    public static void main(String[] args) {

        new TestMenu();

    }

}

Note: I'm speaking of version 1.6.0_15 of Apple's JVM. I have to keep obsolete versions in mind to make sure my software will not expose any data because of bugs in some JVM on user's computer which was not updated since he or she bought that MAC. Current version of Java for Windows and Mac OS X works fine. The question itself: maybe someone knows a way to manually synchronize JMenu and it's representation? Or maybe you can propose another workaround?

Upvotes: 3

Views: 706

Answers (2)

Aleksandr Kravets
Aleksandr Kravets

Reputation: 5947

I've found the solution, and it wasn't so hard...

.................................
        menu.add(item2);
        setBounds(100, 100, 100, 100);
        setVisible(true);
        submenu.add(subitem1);
        submenu.add(subitem2);
        SwingUtilities.updateComponentTreeUI(mb); //This line updates menu representation
    }
................................

Upvotes: 2

Valentino Ru
Valentino Ru

Reputation: 5052

Maybe I understood you wrong, but it's only a problem to display the changes? If so, try repaint() after submenu.add(subitem1) and submenu.add(subitem2).

Upvotes: 1

Related Questions