Double Titlebar for JFrame in Swing

Yeah! I am excited to share this, a silly thing, but weird. Here is how to set a double titlebar for JFrame in swing, one comes with a default look and feel and the other is a normal title bar (based on OS). Now, just see the image before the program.

A JFrame with Double titlebar

import javax.swing.*;
import java.awt.*;
class DoubleTitleBar extends JFrame
{
JButton b;

    public DoubleTitleBar()
    {
        createAndShowGUI();
    }
   
    private void createAndShowGUI()
    {
        setTitle("Double Title bar");
        setLayout(new FlowLayout());
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        b=new JButton("Button");
        add(b);
       
        setSize(400,400);
        setVisible(true);
   
        // This does the thing!!
        getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
    }
   
    public static void main(String args[])
    {
        new DoubleTitleBar();
    }
}

getRootPane().setWindowDecorationStyle(): This method takes an int which can be a FRAME, PLAIN_DIALOG etc and sets the window decoration style, i.e. styles the title bar. Also see using setDefaultLookAndFeelDecorated()

The greatest compliment you can give me is when you share this, I would sincerely appreciate it. :)

“The most beautiful things in the world cannot be seen or even touched, they must be felt with the heart” - Helen Keller


Posted by at on

Tags: Swing Hacks,