3

I am developing a Java Swing-based application with different perspectives. For the "main menu" perspective I do not want the window (JFrame) to be decorated, while in other perspective I do want the window to be decorated. In other words, I need want to change the decoration attribute dynamically.

I have tried to use setUndecorated(false) and setUndecorated(true), but I seems I can only set this once, before actually showing the window.

Is there a way to achieve this?

Guillaume Polet
  • 47,259
  • 4
  • 83
  • 117
magomar
  • 1,038
  • 1
  • 12
  • 26

2 Answers2

3

From the javadoc:

Disables or enables decorations for this frame. This method can only be called while the frame is not displayable.

Therefore, once the JFrame is packed and/or displayed, you can no longer change that value. If you want to change the undecorated state of a JFrame you will need to dispose() it first, then change the state and eventually make it visible again.

Guillaume Polet
  • 47,259
  • 4
  • 83
  • 117
  • It worked as you described, thank you ! The procedure is described in the documentation of `Window.dispose()', but I didn't know where to look for that – magomar Jun 21 '13 at 21:20
0

After all, I had to take a different approach. The former solution did work, as I stated in my last comment. However, it was showing the default LAF window decoration, while I was using a different LAF. So the result was graphically inconsistent with the rest of the LAF. Finally, I came with the right solution, I used setUndecorate(true) for my frame. Then, when I had to change my perspective to one using decorations I simply had to use the following code

contentPane.getRootPane().setWindowDecorationStyle(JRootPane.FRAME);

And when I had to revert to the non decorate perspective, I use

contentPane.getRootPane().setWindowDecorationStyle(JRootPane.NONE);

This approach didn't need to dispose the window and show it again (which actually produced a brief but still visible hide/show of the window)

magomar
  • 1,038
  • 1
  • 12
  • 26
  • Which LAF did this work with? It does not seem to work with the OS X aqua LAF (Java8/OS X 10.9.5). – Hendrik Nov 14 '14 at 14:32