javaswinglook-and-feel

Cannot change color of Titlebar and JMenuBar


I try to modify either the color of the Windows title bar or the color of JMenuBar. I set every look and feel key to Color.GREEN via UIManager.put( "XXXXXXX", Color.GREEN), where XXXXX is just a placeholder. Most elements change but some not. See image for example.enter image description here

The red one is my problem (the others are stange too but okay).

GUI build with netbeans gui builder ( JFrame ->JMenuBar )

Maybe I should mention that:

  1. Used LAF "Windows"
  2. Used OS: Windows 10

SOLUTION UPDATE - TEMPORARY Okay I got something working right now (cant test much will do later). I have to create the JFrame befor changing to Windows LAF, this results in a frame like @bhavna garg and @Ganesh Patel then I change the LAF to windows and all other elements look like I wanted them. The colors are not right and it's not a feasable solution I think but I will check that later


Solution

  • I prefer you can use metal look and feel where I can change the color of title bar as well as change the color of menu bar and menu.

    Here is code :

    import java.awt.Color;
    import javax.swing.JFrame;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JRootPane;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    import javax.swing.plaf.ColorUIResource;
    import javax.swing.plaf.metal.DefaultMetalTheme;
    import javax.swing.plaf.metal.MetalLookAndFeel;
    
    public class MyLookAndFeel {
      JFrame frame;
      JMenuBar menubar;
      MetalLookAndFeel metal;
      JMenu menu;
    
      public MyLookAndFeel() {
        metal = new MetalLookAndFeel();
        metal.setCurrentTheme(new MetalTheme());
        try {
          UIManager.setLookAndFeel(metal);
        }
        catch(UnsupportedLookAndFeelException e) {
          e.printStackTrace();
        }
        frame = new JFrame("Hello");
    
        frame.setUndecorated(true);
        frame.getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
    
        menubar = new JMenuBar();
        menubar.setOpaque(true);
        menubar.setBackground(Color.green);
    
        menu = new JMenu("File");
        menubar.add(menu);
        frame.setJMenuBar(menubar);
    
        frame.setVisible(true);
        frame.setSize(100,100);
    
      }
      public class MetalTheme extends DefaultMetalTheme {
    
        @Override
        public ColorUIResource getMenuBackground() {
          return new ColorUIResource(Color.GREEN);
        }
        public ColorUIResource getWindowTitleBackground() {
            return new ColorUIResource(java.awt.Color.green);
        }
      }
      public static void main(String args[]) {
        new MyLookAndFeel();
      }
    }
    

    You can see the FrameFrame Image