How to call setUndecorated() after a frame is made visible?

48,767

Solution 1

You can't. That's been my experience when I tried to achieve the same.

However if you have your entire UI in one panel which is in your frame, you can create a new frame and add that panel to the frame. Not so much work.

Something like this:

// to start with
JPanel myUI = createUIPanel();
JFrame frame = new JFrame();
frame.add(myUI);

// .. and later ...

JFrame newFrame = new JFrame();
newFrame.setUndecorated();
newFrame.add(myUI);

In Swing a panel (and indeed any instance of a component) can only be in one frame at a time, so when you add it to a new frame, it immediately ceases to be in the old frame.

Solution 2

Have you tried calling Frame.dispose() and then changing it? Haven't tried it myself, but it might work.

If not, then what you can do is have the frame an inconsequential part of the class, with only the most minimal hooks to the highest level panel or panels necessarily, and just move those to the new frame. All the children will follow.

Solution 3

calling dispose() releases the native window resources. then you can edit properties like undecorated and so on. then just call setVisible(true) to recreate the window resources and everything works fine (the position and all compoenents won`t be changed)

dispose();
setUndecorated(true/false);
setVisible(true);

Solution 4

Try:

dispose();
setUndecorated(true);
setVisible(true);

Check it Out. Hope it will help !!

Solution 5

Well, you are going to need different frame instance. You may be able to move the contents of your frame over without recreating that. The key here is to make your code not be reliant on a specific frame. This is a basic good practice in any case.

Share:
48,767
Lucky
Author by

Lucky

Updated on July 11, 2022

Comments

  • Lucky
    Lucky almost 2 years

    In my Swing application, I want the ability to switch between decorated and undecorated without recreating the entire frame. However, the API doesn't let me call setUndecorated() after the frame is made visible.

    Even if i call setVisible(false), isDisplayable() still returns true. The API says the only way to make a frame not-displayable is to re-create it. However, I don't want to recreate the frame just to switch off some title bars.

    I am making a full-screenable application that can be switched between fullscreen and windowed modes; It should be able to switch while maintaining the state, etc.

    How do I do this after a frame is visible?.

  • Lucky
    Lucky about 15 years
    That's what I've considered but I'm looking for an alternative.
  • Thomas
    Thomas over 11 years
    calling dispose() releases the native window resources. then you can edit properties like undecorated and so on. with pack() you recreate the window resources. then just call setVisible(true) and everything works fine (the position and all compoenents won`t be changed)
  • maq
    maq over 9 years
    its amazing and simple solution thank you so much i did this dispose(); 'setUndecorated(true); setExtendedState(JFrame.MAXIMIZED_BOTH); setVisible(true);'
  • Christian Hujer
    Christian Hujer over 9 years
    As Thomas described, this is possible, see also stackoverflow.com/questions/27715638/…
  • PaulProgrammerNoob
    PaulProgrammerNoob over 7 years
    This should be the accepted answer as it doesn't create a new JFrame every time.