11

I am using the following code,

UIManager.put("JFrame.activeTitleBackground", Color.red);

for change the toolbar color in JFrame. But it didn't work.

Is it possible to change the color of titlebar in JFrame?

Arsh Coder
  • 688
  • 9
  • 33
Venkat
  • 20,802
  • 26
  • 75
  • 84

7 Answers7

6
//source : javax/swing/plaf/metal/MetalTitlePane.java
    import javax.swing.*;
    import javax.swing.plaf.*;
    import javax.swing.plaf.metal.*;
    public class TitleColor
    {
        public static void main_helper (String args[])
        {
            JFrame f = new JFrame ();
            f.setDefaultCloseOperation 
            (
                JFrame.DISPOSE_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 );

            DefaultMetalTheme z =
            new DefaultMetalTheme ()
            {
                //inactive title color
                public ColorUIResource
                getWindowTitleInactiveBackground() 
                { 
                    return new ColorUIResource 
                        (java.awt.Color.orange); 
                }

                //active title color
                public ColorUIResource
                getWindowTitleBackground() 
                { 
                    return new ColorUIResource 
                        (java.awt.Color.orange); 
                }
                //start ActiveBumps
                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); 
                }
                //end ActiveBumps

                //start inActiveBumps
                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); 
                }
                //end inActiveBumps



            };



            MetalLookAndFeel.setCurrentTheme
            (
                z
            );

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

            SwingUtilities.updateComponentTreeUI (f);


            f.setVisible (true);


        }
        public static void main (final String args[])
        {
            SwingUtilities.invokeLater
            (
                new Runnable ()
                {
                    public void run ()
                    {
                        main_helper ( args );
                    }
                }
            );
        }
    }
guest123
  • 71
  • 1
  • 5
  • 2
    Good example. However this not only changes color, but installs a new LAF, which renders the application looking like back in the '90s. :) – Gee Bee Mar 07 '18 at 22:25
6

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.

Andrew Dyster
  • 2,278
  • 19
  • 23
  • 2
    It is possible with some look-and-feel packages or custom look-and-feel code. There is an example below from `user1781452`. – Enwired Oct 20 '16 at 22:35
  • 1
    With Java 8 the [answer using `UIDefaults` with the key `activeCaption`](https://stackoverflow.com/a/49019710/2107731) works perfectly. – Claude Nov 02 '18 at 07:38
4

For Windows10, in your main method, you can use:

UIDefaults uiDefaults = UIManager.getDefaults();
uiDefaults.put("activeCaption", new javax.swing.plaf.ColorUIResource(Color.gray));
uiDefaults.put("activeCaptionText", new javax.swing.plaf.ColorUIResource(Color.white));
JFrame.setDefaultLookAndFeelDecorated(true);
2

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 and controlled by 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
2

Yes I found the solution. I think it may be helpful for people who are seeing this now.

Use FlatLaf

In that there are many look and feels. You can see them here.

To change the title bar background color and foreground color use this:-

FlatLightLaf.setup(); //setting the look and feel
JFrame.setDefaultLookAndFeelDecorated(true);
frame.getRootPane().putClientProperty("JRootPane.titleBarBackground", new Color(23,180,252));
       frame.getRootPane().putClientProperty("JRootPane.titleBarForeground", Color.white);

Now I can achieve this:-

enter image description here

Arsh Coder
  • 688
  • 9
  • 33
0

I think the goal is to achive a real application look on Win10. As it is not possible to change the window title color for real, the only way is to customize the window.

Although examples installing a variant of the Metal LAF gives a good example, I found that this problem is more complicated. A real Win10 window has to have win10 border, transparent border (shadow) where the user can drag for resize. The title has to use Win10 icons, and hovers for the window buttons.

I'd f.setUndecorated(true); and draw it on my own, and set the insets of the window so that the content will work as normal.

(Small print: although we all "know" that one can customize Swing with LAFs, in the real life writing a LAF is always a lot more complicated than just subclassing and drawing your own decoration. As an extra hassle, the LAF architecture does not express all the component properties, and the "native" LAF is quite far from the native look (e.g. check the win7 dropdown) or the feel (dropdowns don't slide out, win7 dropdowns have no hover, but buttons do). Oh, not to mention the lack of Windows-like components, such as a decent Office 2016 ribbon, or even a simple "toggle" of Win10. Really, you cannot do too much without brewing your own components.)

Gee Bee
  • 1,794
  • 15
  • 17
-1
UIManager.put("InternalFrame.activeTitleBackground", new ColorUIResource(Color.black ));
UIManager.put("InternalFrame.activeTitleForeground", new ColorUIResource(Color.WHITE));
UIManager.put("InternalFrame.titleFont", new Font("Dialog", Font.PLAIN, 11));
Hans Olsson
  • 54,199
  • 15
  • 94
  • 116
yassine
  • 15
  • 2