How to change the color of Title Bar in JFrame in Java
In this tutorial, we will learn how can we change the color of the title bar in JFrame in Java.
Now let’s talk about what is JFrame. JFrame is a type of container imported from javax.swing package which inherits the java.awt.Frame class. When a JFrame is created, it develops a window that works like the main window where all the components like JLabel, JButton, JTextBoxes are inserted or added to created a Java GUI Application. Every JFrame window has a default Title bar.
This tutorial is all about how to change the color or design of the JFrame Title bar.
Firstly, we should create a JFrame window.
How to create a JFrame Window using Java swing
import javax.swing.*; import java.io.*; import java.awt.*; class titlebar extends JFrame{ titlebar(){ //Title of the Window setTitle("JFrame window"); //setting the location and size of the JFrame Window setBounds(300,100,600,500); //Default close operation when close button is clicked the java program excecution stops setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // By default JFrame window is invisible so setting it to true setVisible(true); } public static void main(String[] args) { //calling the titlebar constructor of titlebar class new titlebar(); } }
Output:
So the above output is the JFrame Window having Default Title Bar.
Let’s write a simple code snippet for changing the color of the title bar.
Code:
try{ UIManager.put("JFrame.activeTitleBackground", Color.red); } catch(Exception e){ e.printStackTrace(); }
Here UIManager class imported from javax.swing.UIManager is used to set and control the look and feel of the Java GUI. Although this class controls the look and feel still it has some limitations. It cannot alter the top-level part of JFrame (Title Bar) as the look and feel are controlled by the underlying operating system and not by Java. In simple words, it can be concluded that the default Title bar cannot have different colors. However you can change the color of the JInternal Frame which can be operated as the main Title bar and the default title bar can be removed using the snippet ( setUndecorated(true)) .
Code for the above description:
import javax.swing.*; import javax.swing.plaf.ColorUIResource; import javax.swing.plaf.metal.DefaultMetalTheme; import javax.swing.plaf.metal.MetalLookAndFeel; class ColorTitleBar extends JFrame{ ColorTitleBar(){ setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(700,700); setLocationRelativeTo(null); setUndecorated(true);// vanishing the default title bar //Setting the RootPane as the main window frame getRootPane().setWindowDecorationStyle(JRootPane.FRAME); setTitle("HELLO"); JPanel panel = new JPanel(); panel.setBackground(java.awt.Color.white); setContentPane(panel); panel.setLayout(null); panel.setBounds(100,100,400,400); //Hello World JLabel JLabel xyz=new JLabel("Hello World"); xyz.setBounds(310,40,100,200); panel.add(xyz); MetalLookAndFeel.setCurrentTheme(new changeTheme()); //UIManager code snippet to set the look and feel them for the window try { UIManager.setLookAndFeel(new MetalLookAndFeel()); } catch (Exception e) { e.printStackTrace(); } //A simple minded look and feel change that is to initialize its UI property with the current look and feel. SwingUtilities.updateComponentTreeUI(this); setVisible(true); } public static void main(String[] args) { new ColorTitleBar();//calling ColorTitleBar } } //Theme Class to Change the default color to green color class changeTheme extends DefaultMetalTheme { public ColorUIResource getWindowTitleInactiveBackground() { return new ColorUIResource(java.awt.Color.green); } public ColorUIResource getWindowTitleBackground() { return new ColorUIResource(java.awt.Color.green); } public ColorUIResource getPrimaryControlHighlight() { return new ColorUIResource(java.awt.Color.green); } public ColorUIResource getPrimaryControlDarkShadow() { return new ColorUIResource(java.awt.Color.green); } public ColorUIResource getPrimaryControl() { return new ColorUIResource(java.awt.Color.green); } public ColorUIResource getControlHighlight() { return new ColorUIResource(java.awt.Color.green); } public ColorUIResource getControlDarkShadow() { return new ColorUIResource(java.awt.Color.green); } public ColorUIResource getControl() { return new ColorUIResource(java.awt.Color.green); } }
Output:
By using the above code we not only removed the default title bar but also have a customized title bar with changed color. This happened because the RootPane of Java swing is not controlled by the underlying OS and can be manipulated using java code. Yes, the color has changed but I would suggest using the default title bar so that this complex task is avoided and developers can work on real problems.
KEEP Learning !!!!!
Leave a Reply