Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JMenu not in Windows look and feel

Hi I've got a little Swing Application with a Menu. First two attributes containing the menues text are created, then the lookandfeel is set to windows and at last the menues are filled. Here is the source code:

private JMenu[] Menue={new JMenu("File")};

private JMenuItem[][] MenuItemsString ={{new JMenuItem("Import"),new JMenuItem("Export")}};
...
public window(){
       super ("Q3MeshConverter");

       plate = new JPanel(new BorderLayout());
       try{
           UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");// set Windows lookandfeel
           }
           catch(Exception x){

           }
       menuBar = new JMenuBar();
       ...
       setJMenuBar(menuBar);

       JMenu[] Menu =Menue;
       JMenuItem[][] MenuItems =MenuItemsString;
       for(int m=0;m<Menu.length;m++){// loop trough the Menu(es)
           menuBar.add(Menu[m]);
           for(int mi=0;mi<MenuItems[m].length;mi++){// loop through the MenuItems
               Menu[m].add(MenuItems[m][mi]);
               MenuItems[m][mi].addActionListener(this);
           }
       }
       ...   
       setContentPane (plate);
}

And that's the ugly output:

Why does it looks like this?

like image 814
Martin Erhardt Avatar asked Dec 04 '25 22:12

Martin Erhardt


1 Answers

There is no magic how a component created before the LAF change can know about it, you have to tell it :-)

SwingUtilities.updateComponentTreeUI(someComponent);
like image 139
kleopatra Avatar answered Dec 11 '25 19:12

kleopatra