2

I want to create a customised title bar for my JFrame. I can remove the default title bar with

JFrame.setUndecorated(true)

Now i need to create a customised title bar for my JFrame with a close button?

Neuron
  • 5,141
  • 5
  • 38
  • 59
boopathy
  • 427
  • 2
  • 9
  • 20

4 Answers4

3

Without having done that ever, I think I would go this way:

  1. Indeed set the JFrame to undecorated
  2. Extend JRootPane to add an additional field titleBar
  3. Create a TitleBar component holding the title, the close button, etc...
  4. Set a new LayoutManager on that JRootPane (have a look at JRootPane.RootLayout) and layout the components in the appropriate order (first the title bar, then below the menubar, then below the content pane)
  5. Set an instance of that extends RootPane on your JFrame

There are maybe better ways.

Neuron
  • 5,141
  • 5
  • 38
  • 59
Guillaume Polet
  • 47,259
  • 4
  • 83
  • 117
0

I'm not quite sure of how you want to customize the close button, but maybe this can point you in the right direction: How can I customize the title bar on JFrame?

EDIT: Here's an updated working link to a forum about customizing his GUI and one user posted code on his creation of a simple GUI: Here

It looks like you can just modify his removeComponents method and create an addComponents method to fit your needs.

Community
  • 1
  • 1
persinac
  • 66
  • 1
  • 7
  • 1
    Yeah but the answers in that questions refer themselves to other links which don't work anymore. Please post direct working links or delete this post – Guillaume Polet Oct 10 '12 at 14:50
0

The Code According to the Above Link : (Edited for Java 8)

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.logging.Level;
import java.util.logging.Logger;

class Testing {
  public void buildGUI() throws UnsupportedLookAndFeelException {
    JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame f = new JFrame();
    f.setResizable(false);
    removeMinMaxClose(f);
    JPanel p = new JPanel(new GridBagLayout());
    JButton btn = new JButton("Exit");
    p.add(btn, new GridBagConstraints());
    f.getContentPane().add(p);
    f.setSize(400, 300);
    f.setLocationRelativeTo(null);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
    btn.addActionListener((ActionEvent ae) -> {
      System.exit(0);
    });
  }

  public void removeMinMaxClose(Component comp) {
    if (comp instanceof AbstractButton) {
      comp.getParent().remove(comp);
    }
    if (comp instanceof Container) {
      Component[] comps = ((Container) comp).getComponents();
      for (int x = 0, y = comps.length; x < y; x++) {
        removeMinMaxClose(comps[x]);
      }
    }
  }

  public static void main(String[] args) {
    SwingUtilities.invokeLater(() -> {
      try {
        new Testing().buildGUI();
      } catch (UnsupportedLookAndFeelException ex) {
        Logger.getLogger(Testing.class.getName()).log(Level.SEVERE, null, ex);
      }
    });
  }
}

may Work Fine but what if user also Want to set a L&F such as nimbus

Leon
  • 3,124
  • 31
  • 36
MAK
  • 163
  • 2
  • 11
0

There are really three ways to approach this:

  1. Set the frame to undecorated and implement everything, which includes control buttons, snapping, resizing and moving.
  2. Get the root pane of the JFrame and directly edit that pane. You will need to add the control buttons and the snapping behaviour.
  3. Use JNI to get the window's handle at the creation of a JFrame to get the control of it's attributes. This is better explained in this post. I have also built a little project which is basically an extension of the JFrame class that handles everything that needs to be dealt with... This last approach does not break native functions like snapping and resizing. But you do need to create the control buttons again since you have a new title bar if you want to build it from scratch.
Frederic Perron
  • 777
  • 4
  • 19