JMenu does not look and feel in Windows

Hi I have a small Swing app with a menu. The first two attributes are created containing the text menues, then the lookandfeel is set to windows and finally the menu is populated. Here's 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 what an ugly way out:

Why does it look like this?

+3


source to share


2 answers


Set the appearance of your method main

:



public static void main(String[] args) {
    try {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (Exception e) { }
}

      

+4


source


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



SwingUtilities.updateComponentTreeUI(someComponent);

      

+8


source







All Articles