Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove title bar in JFrame

I'm using the following code for practising,

http://docs.oracle.com/javase/tutorial/uiswing/examples/layout/BorderLayoutDemoProject/src/layout/BorderLayoutDemo.java

I also add

frame.setSize(frame.getMaximumSize());

in createAndShowGUI() method,

What is more I want this window not to have the title bar, close and minimize buttons.

I tried the following code,

frame.setUndecorated(true);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

If I added this code before the pack(), it goes into infine loop with this exception Exception in thread "AWT-EventQueue-0" java.lang.NegativeArraySizeException

If I added the last line of createAndShowGUI() method it throws Exception in thread "AWT-EventQueue-0" java.awt.IllegalComponentStateException: The frame is displayable.

What should I do ?

Thanks.

like image 994
CanCeylan Avatar asked Jan 02 '12 13:01

CanCeylan


People also ask

How to hide title bar JFrame?

The title bar can be removed by calling setUndecorated(true) on the Frame or JFrame instance, i.e. by setting the "undecorated" property to true . This removes both the title bar and the surrounding frame.

How do I remove a label from GUI in Java?

add(label); final JButton remove = new JButton("Remove label"); leftPanel. add(remove); add. addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { rightPanel. remove(label); } });

How do I hide a JFrame?

Just add this: JFrame. setDefaultCloseOperation(DISPOSE_ON_CLOSE) . Note: The default option for JFrame is HIDE_ON_CLOSE . This will cause the program to slow down much like setVisible(false); .

How do I add a title to a JFrame?

First, you can set the JFrame title when you construct your JFrame, like this: JFrame jframe = new JFrame("My JFrame Title"); Second, once you have a valid JFrame object, you can call the setTitle method of the JFrame class, like this: jframe.


2 Answers

The title bar can be removed by calling setUndecorated(true) on the Frame or JFrame instance, i.e. by setting the "undecorated" property to true. This removes both the title bar and the surrounding frame.

Here's the required code for the question:

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Already there
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setUndecorated(true); // <-- the title bar is removed here

On a packed JFrame

setUndecorated(true) must be called before pack() is being called, either directly or indirectly, for instance through setVisible(true).

Apparently it is not possible to remove the title bar after the packing is performed, this is the documentation of Frame#setDecorated(boolean):

A frame may have its native decorations (i.e. Frame and Titlebar) turned off with setUndecorated. This can only be done while the frame is not displayable. The method will throw a runtime exception: IllegalComponentStateException if the frame is displayable.

However, you can call Window#dispose() on the Frame or JFrame and then pack it again, which causes the layout of the components within the JFrame to be refreshed.

like image 111
Joop Eggen Avatar answered Oct 13 '22 00:10

Joop Eggen


Well, the following code snippet in createAndShowGUI() worked for me:

JFrame frame = new JFrame("BorderLayoutDemo");
frame.setUndecorated(true); // Remove title bar
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addComponentsToPane(frame.getContentPane());
frame.pack();
frame.setVisible(true);

Note that I'm not sure what you're trying to achieve by manually setting the size of an unrealized frame to it's maximum size, which will be (0, 0) initially.

like image 7
mre Avatar answered Oct 13 '22 00:10

mre