How to remove title bar in 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.

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.

Leave a Comment