Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the color of titlebar in JFrame?

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?

like image 314
Venkat Avatar asked Mar 20 '10 12:03

Venkat


3 Answers

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

like image 170
JFan Avatar answered Oct 30 '22 16:10

JFan


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.

like image 35
Andrew Dyster Avatar answered Oct 30 '22 16:10

Andrew Dyster


//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 );
                    }
                }
            );
        }
    }
like image 43
guest123 Avatar answered Oct 30 '22 18:10

guest123