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

吃可爱长大的小学妹 提交于 2019-11-27 05:00:00

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.

Yishai

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.

Have a look at https://tvbrowser.svn.sourceforge.net/svnroot/tvbrowser/trunk/tvbrowser/src/tvbrowser/ui/mainframe/MainFrame.java

In Method switchFullscreenMode():

dispose();
...
setFullScreenWindow(...);
setUndecorated(true/false);
setBounds(mXPos, mYPos, mWidth, mHeight);
...
setVisible(true);

Actually there's a lot more stuff going on to hide various sidepanels that reappear if the mouse touches the sides.

Also note that you must explicitly set the bounds. Window.setExtendedState(MAXIMIZED_BOTH) interferes badly in timely vicinity of dispose(), because they both rely on multiple native events of the operating system, that are lost, should the window no be displayable at that split second.

I don't recommend taking the default screen directly:

GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();

and instead use the Screen, your JFrame is currently on:

setBounds(getGraphicsConfiguration().getBounds());
getGraphicsConfiguration().getDevice().setFullScreenWindow(this);

Though it's currently the same, it might change in the future.

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);

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.

Deepak Karamchandani

Try:

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

Check it Out. Hope it will help !!

Eng.Eyad S.

Here is a code in how to make ALT + Enter enters Full Screen without the bar mode and Minimize with showing the Title bar and the Start bar:

public class myTest extends JFrame{
 //Your codes...
     //if "ALT" key on hold and "Enter" key pressed with it
     if (evt.isAltDown() && evt.getKeyCode() == evt.VK_ENTER) {    
         //if the JFrame has Title bar
         if (isUndecorated()) {
             //this will dispose your JFrame
             dispose();
             //here to set it with no Title bar
             setUndecorated(false);
             pack();
             setLocationRelativeTo(null);
             //as you dispose your JFrame, you have to remake it Visible..
             setVisible(true);
          } else {
             dispose();
             setUndecorated(true);
             setExtendedState(MAXIMIZED_BOTH);
             setVisible(true);
        }
    }
//your codes
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!