4

I'm working on application and I'm making the GUI with Swing. I would like my application to be fullscreen. I can easily set the size of the window however I have not been able to make the application truly fullscreen (IE With the apple menu bar and dock hidden). All the answers I have found online don't seem to work for me. I'm new to Java so any help is appreciated.

frame = new JFrame("Test");
    frame.setTitle("Test");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JLabel emptyLabel = new JLabel("");
    Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
    frame.getContentPane().add(emptyLabel, BorderLayout.CENTER);
    frame.setSize((int)dimension.getWidth(), (int)dimension.getHeight());
    int x = (int) ((dimension.getWidth() - frame.getWidth()) / 2); // X center
    int y = (int) ((dimension.getHeight() - frame.getHeight()) / 2);  //Y center
    frame.setLocation(x, y); //Set Frame Location
    frame.setResizable(false); //Frame is not resizable
    frame.setUndecorated(true);  //No decoration for the frame
    frame.setAlwaysOnTop(true);
    frame.setVisible(true); //Make visible
mKorbel
  • 109,525
  • 20
  • 134
  • 319
iicaptain
  • 1,065
  • 1
  • 13
  • 37
  • 1
    Fullscreen support for mac OS can (or at least could be) achieved using something like [this](http://stackoverflow.com/questions/6873568/fullscreen-feature-for-java-apps-on-osx-lion) and [this](http://stackoverflow.com/questions/23924553/setting-fullscreen-using-other-ways-besides-getting-screen-size-in-java/23924624#23924624) this will provide the user with the experience that they would normally expect. Otherwise you will need to use the [Full screen exclusive mode](https://docs.oracle.com/javase/tutorial/extra/fullscreen/) provided by Java API – MadProgrammer May 07 '15 at 00:40
  • *"All the answers I have found online don't seem to work for me"* - Perhaps you would like to provide an example of what hasn't worked for you and why – MadProgrammer May 07 '15 at 00:41
  • @MadProgrammer I actually tried the first answer you linked me and it threw some errors. I just tried the second. I added this code: `GraphicsEnvironment ge = GraphicsEnvironment .getLocalGraphicsEnvironment(); GraphicsDevice myDevice = ge.getDefaultScreenDevice(); myDevice.setFullScreenWindow(frame); ` and it worked – iicaptain May 07 '15 at 01:03
  • Remember, the first example is ONLY for MacOS ;) – MadProgrammer May 07 '15 at 01:04

1 Answers1

6

Full screen support under Windows and MacOS have different user expectations...

You could use Full screen exclusive mode on both, but Mac users have a different exceptions when it comes to full screen applications, as MacOS supports full screen applications at an OS level

I tested the following code (which is based on this example) on Mavericks with Java 8 and it works fine.

public static void enableOSXFullscreen(Window window) {
    try {
        Class util = Class.forName("com.apple.eawt.FullScreenUtilities");
        Class params[] = new Class[]{Window.class, Boolean.TYPE};
        Method method = util.getMethod("setWindowCanFullScreen", params);
        method.invoke(util, window, true);
    } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | ClassNotFoundException ex) {
        ex.printStackTrace();
    }
}

public static void requestOSXFullscreen(Window window) {
    try {
        Class appClass = Class.forName("com.apple.eawt.Application");
        Class params[] = new Class[]{};

        Method getApplication = appClass.getMethod("getApplication", params);
        Object application = getApplication.invoke(appClass);
        Method requestToggleFulLScreen = application.getClass().getMethod("requestToggleFullScreen", Window.class);

        requestToggleFulLScreen.invoke(application, window);
    } catch (ClassNotFoundException | NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
        ex.printStackTrace();
    }
}

One of the hardest hurdles you will have with users accepting your application is working with their current expectations. Do something they aren't use to and no matter how wonderful your app is, they won't like you for it (IMHO).

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • See also this related [example](http://stackoverflow.com/a/30308671/230513) using `requestToggleFullScreen()`. – trashgod May 18 '15 at 16:45
  • Your code works fine for me, but I cannot understand why you have used reflection instead of the straightforward `myTargetGraphicsDevice.setFullScreenWindow( myJFrame.getWindows()[0] )`. Could you possibly explain? I don't see how this relates to user expectations. – Douglas Held Aug 27 '17 at 23:39
  • 1
    Because that's not the functionality that I used, *"Full screen support under Windows and MacOS have different user expectations..."* - I've used an API which is only available under Mac OS which supports it's "full screen" concept. Because of this, I've had to use reflection - I'd also add a check to see if you're running in Windows or not and then decide which route you want to use, but that's beyond the scope – MadProgrammer Aug 28 '17 at 00:23