dark_982
dark_982

Reputation: 192

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

Upvotes: 1

Views: 3896

Answers (4)

Ali Fanni
Ali Fanni

Reputation: 31

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

Upvotes: 1

dark_982
dark_982

Reputation: 192

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

Upvotes: 2

Ganesh Patel
Ganesh Patel

Reputation: 450

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

Upvotes: 3

bhavna garg
bhavna garg

Reputation: 270

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);

Upvotes: 3

Related Questions