How to Change the Color of Titlebar in Jframe

How to change the color of titlebar in JFrame?

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

You CAN change the color of an InternalFrame.

How to change the color of the title bar in javax.swing?

I think it's not possible. Because the top-level JFrame acquires the look & feel of the machine's operating system.

By the way this program will help you to change the frame appearance with the help of LAF (Look And Feel):

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRootPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.plaf.ColorUIResource;
import javax.swing.plaf.metal.DefaultMetalTheme;
import javax.swing.plaf.metal.MetalLookAndFeel;

public class Main {
public static void main(final String args[]) {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(300, 300);
f.setLocationRelativeTo(null);

f.setUndecorated(true);
f.getRootPane().setWindowDecorationStyle(JRootPane.FRAME);

JPanel panel = new JPanel();
panel.setBackground(java.awt.Color.white);
f.setContentPane(panel);

MetalLookAndFeel.setCurrentTheme(new MyDefaultMetalTheme());
try {
UIManager.setLookAndFeel(new MetalLookAndFeel());
} catch (Exception e) {
e.printStackTrace();
}

SwingUtilities.updateComponentTreeUI(f);

f.setVisible(true);
}
}

class MyDefaultMetalTheme extends DefaultMetalTheme {
public ColorUIResource getWindowTitleInactiveBackground() {
return new ColorUIResource(java.awt.Color.orange);
}

public ColorUIResource getWindowTitleBackground() {
return new ColorUIResource(java.awt.Color.orange);
}

public ColorUIResource getPrimaryControlHighlight() {
return new ColorUIResource(java.awt.Color.orange);
}

public ColorUIResource getPrimaryControlDarkShadow() {
return new ColorUIResource(java.awt.Color.orange);
}

public ColorUIResource getPrimaryControl() {
return new ColorUIResource(java.awt.Color.orange);
}

public ColorUIResource getControlHighlight() {
return new ColorUIResource(java.awt.Color.orange);
}

public ColorUIResource getControlDarkShadow() {
return new ColorUIResource(java.awt.Color.orange);
}

public ColorUIResource getControl() {
return new ColorUIResource(java.awt.Color.orange);
}
}

Cannot change color of Titlebar and JMenuBar

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

Setting the Color of the Title Bar with Netbeans Platform Application (FlatLaF)

I believe I found the culprit.
In a recent release of Netbeans the option to set "TitlePane.unifiedBackground" for FlatLaF was introduced.
However when setting UIManager.put("TitlePane.unifiedBackground", true); the problem described above will occur and I effectively can't set the background color of the TitlePane.

As seen in the method updateUnifiedBackground() of class FlatLFCustoms(https://github.com/apache/netbeans/blob/12.4/platform/o.n.swing.laf.flatlaf/src/org/netbeans/swing/laf/flatlaf/FlatLFCustoms.java) in the Netbeans source code, the color of the titlePane should be set by adjusting the property "Panel.background".
This however doesn't seem to work properly.

A workaround seems to be to set UIManager.put("TitlePane.unifiedBackground", false); and then just set the "TitlePane.background" to the desired color.
If you still want the unified TitlePane look you can achieve that by also setting "Panel.background" and "Toolbar.background" to the same color.

In effect this will look identical to setting the unifiedBackground for the TitlePane only now you can change the color.
Hope this helps someone who stumbles upon the same Problem.

java titlebar modification

I think you can change the title bar color with the method described here: http://www.coderanch.com/t/346141/GUI/java/set-JFrame-titlebar-color

Changing the text should be possible via setTitle(). You can call this method as many times as you want throughout the life of the application to change the title text on the fly.



Related Topics



Leave a reply



Submit