Wednesday, May 2, 2012

Enabling Full Screen Mode for Java Applications Running on Mac OS X Lion

One of the notable features in Mac OS X Lion is the ability to run applications in full screen mode. With the latest updates to Java on Mac OS X, Java applications are no exception to utilize this nice feature. As with other Java - OS X integration features, the key to using full screen mode lies in the Apple Java Extensions. The latest version of Apple Java Extensions added a new class, com.apple.eawt.FullScreenUtilities, which enables any Window in a Java application to be marked as full screen capable. Let us now take a look at the sample code which demonstrates how to enable full screen mode for Java application windows.

package com.myjavaworld.fullscreendemo;

import java.awt.Window;
import java.lang.reflect.Method;

import javax.swing.JFrame;
import javax.swing.WindowConstants;

public class FullScreenDemo {
    public static void main(String[] args) {
        if (isMacOSX()) {
            System.setProperty(
                    "com.apple.mrj.application.apple.menu.about.name",
                    "Full Screen Demo");
        }
        JFrame frame = new JFrame("Full Screen Demo");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setSize(600, 400);
        if (isMacOSX()) {
            enableFullScreenMode(frame);
        }
        frame.setVisible(true);
    }

    public static void enableFullScreenMode(Window window) {
        String className = "com.apple.eawt.FullScreenUtilities";
        String methodName = "setWindowCanFullScreen";

        try {
            Class<?> clazz = Class.forName(className);
            Method method = clazz.getMethod(methodName, new Class<?>[] {
                    Window.class, boolean.class });
            method.invoke(null, window, true);
        } catch (Throwable t) {
            System.err.println("Full screen mode is not supported");
            t.printStackTrace();
        }
    }

    private static boolean isMacOSX() {
        return System.getProperty("os.name").indexOf("Mac OS X") >= 0;
    }
}

When the above code is executed, an empty JFrame titled Full Screen Demo is displayed as shown in the screenshot below:

Screenshot of the demo application
Screenshot of the Demo Application. Notice the full-screen button at the top-right corner of the window 

To enable the native full screen mode capability, on line 20, we are calling the enableFullScreenMode method. This method uses reflection and in turn calls the static method setWindowCanFullScreen(Window, boolean) in the class com.apple.eawt.FullScreenUtilities. The purpose of using reflection is to avoid any compile time dependencies on Apple Java Extensions. Also, note that we call this method only if the application is running on Mac OS X. Any exceptions (e.g. ClassNotFoundException) that occur during the execution of this method are ignored so the application can still function without the full screen mode support.

References

OS X Lion - Full Screen Apps
Announcement from Apple on the availability of Full Screen Support for Java Applications
Apple Java Extensions home page
Apple Java Extensions API Documentation