1

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

TylerH
  • 20,799
  • 66
  • 75
  • 101
dark_982
  • 171
  • 2
  • 13
  • 1) How to change titlebar color? [You can't](https://stackoverflow.com/questions/2482971/how-can-i-change-the-color-of-titlebar-in-jframe) as it's determined by the OS L&F. 2) For the `JMenuBar`, try the answers in [this question](https://stackoverflow.com/questions/15648030/change-background-and-text-color-of-jmenubar-and-jmenu-objects-inside-it). – Frakcool Jul 19 '17 at 21:26
  • 3) `UIManager.put( "XXXXXXX", Color.GREEN)` there's no property called `"XXXXXXX"` in the [Swing UI Properties](https://stackoverflow.com/questions/1951558/list-of-java-swing-ui-properties). 4) Your user will hate you if you leave that green background, (s)he'll have to use sunglasses to use your program... 5) If all the above tips don't help, consider posting a proper [mcve] – Frakcool Jul 19 '17 at 21:27
  • XXXXX is just a placeholder to descripe which function I used and as I mentioned I changed EVERY key (Mentioned here http://www.http://nadeausoftware.com/articles/2008/11/all_ui_defaults_names_common_java_look_and_feels_windows_mac_os_x_and_linux#ArrowButton) that has a color Type to green (for testing my ui will not stay green forever. – dark_982 Jul 20 '17 at 07:54
  • @Frakcool I tried the answers (especally the marked) one but it didn't worked. – dark_982 Jul 20 '17 at 07:59
  • Okay, I think the only feasable solution is to create a synth look and feel and desing everything by myself. But I still think it's weird that the WIndows LAF ignores settings. But thanks for your help. – dark_982 Jul 20 '17 at 19:55

4 Answers4

3

See for titlebar See This imageTo set color of the titlebar use:

  frame.getRootPane().setWindowDecorationStyle(5);

5 is the constant that will give you a green color to menubar. You can use numbers between 1 to 8. Example: 8 will give orange color.

To set color of menubar use:

 menubar.setBackground(Color.RED);
 menubar.setOpaque(true);
bhavna garg
  • 270
  • 2
  • 19
3

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

Ganesh Patel
  • 450
  • 5
  • 15
  • That works fine, but the look of some elements in windows mode is nicer, like the slider, they look nice in windows. – dark_982 Jul 20 '17 at 11:40
  • 1
    You don't like the few Element you can give your own UI by creating new custom UI class ` extends BasicComponent_NameUI` class for that component and call `setUI()`. Here is the Example :[link](https://stackoverflow.com/a/8209911/5855946) – Ganesh Patel Jul 20 '17 at 12:37
  • Maybe thats a solution, I could create a new Implementation of JMenuBar like in the link you provided. But it's what's the reason I cannot change the color of Jmenu and JTabbedPane. – dark_982 Jul 20 '17 at 13:04
2

I switched to JavaFX. The original conzept didn't worked for me, but thanks for your help.

dark_982
  • 171
  • 2
  • 13
1

I know I'm a bit late, but this may help others:

I searched the whole internet to find the way to change the entire theme of my swing-based app. then I found an article about doing so:

Somebody may say:

Its not possible. The top level JFrame is controlled by the look & feel of the underlying OS.

If you want to access the title bar, first, you should have the access to the native java engine libraries. Fortunately, there is a way to it:

First Step: JNA provides Java programs easy access to native shared libraries without writing anything but Java code. So you can use this repository to access to them: JNA.

Scroll down to readme and find the downloadable library.

Some elements may defer based on your platform you are using. So make sure you use jna-platform-5.8.0.jar to make your app compatible to any platforms.

Second Step: If you don't know how to use JNA libraries, then you could use this as an example: example

anyway, this could solve your problem about title bar color ;)

Main Article: External Link

Help me improve my writing in English by telling me my mistakes :D

Ali Fanni
  • 31
  • 4