4

I am making a simple bible reader, and I want to have a fullscreen option. By default, the frame is maximized, but the frame is there. I have a method, setFullScreen, that removes decoration. However, it does not seem to update after being initialized. Is there a way around this?

setFullScreen method:

public void setFullScreen() {
    mainFrame.setUndecorated(true);
}

Part of the main method

UI book = new UI();
book.setLabelText(1);
book.setFullScreen();

At the same time, setLabelText will behave similarly; once I set it the first time, I can't change it.

CaffeineToCode
  • 830
  • 3
  • 13
  • 25
  • 2
    No, a frame must not be realised (displayable on the screen/connected to a native peer) in order to remove the decoration. – MadProgrammer Dec 31 '14 at 03:11
  • How would I work around this? – CaffeineToCode Dec 31 '14 at 03:14
  • Make two frames, one decorated, one undecorated. Only one of them should be visible at any given time. If your entire user interface is in a single panel, you can move it from one frame to another simply by adding that panel to the desired frame, since that will automatically remove it from any previous parent. – VGR Dec 31 '14 at 03:24
  • Yes it's a duplicate. The accepted answer of that other question is wrong. I don't know the "official procedure" in that case, so I simply posted my answer here despite the duplicate. – Christian Hujer Dec 31 '14 at 03:28
  • @ChristianHujer I would put a comment under the 'wrong' answer linking to yours here. – user207421 Dec 31 '14 at 05:41

1 Answers1

8

The method setUndecorated() can only be used if the frame is not displayable. What you could do is make your frame not displayable by calling dispose().

Your method setFullScreen() could look like this:

public void setFullScreen() {
    mainFrame.dispose();
    mainFrame.setUndecorated(true);
    mainFrame.setVisible(true);
}

Depending on your frame contents, you might want to deal with pack() and / or setSize() explicitly to get best results.

By the way, if you want it to be fullscreen / undecorated always, you could simply ensure that you call mainFrame.setUndecorated(true) before you make the frame displayable. The frame is made displayable by methods such as show(), pack() and setVisible(true).

Christian Hujer
  • 17,035
  • 5
  • 40
  • 47
  • Works, but the transition shows the closing animation of the window, and then displays the fullscreen. – CaffeineToCode Dec 31 '14 at 03:39
  • That's unavoidable. It will not get better than that, that's how the classes `Window` and `Frame` (`JFrame` is just derived from them) work. I don't know of any pure Java application which you can put in fullscreen mode which does not "flash" when enabling or disabling fullscreen. – Christian Hujer Dec 31 '14 at 03:42