Skip to content

Instantly share code, notes, and snippets.

@d42ohpaz
Created December 4, 2012 05:24
Show Gist options
  • Save d42ohpaz/4200907 to your computer and use it in GitHub Desktop.
Save d42ohpaz/4200907 to your computer and use it in GitHub Desktop.
Mac OS X Java Toggle Fullscreen Programmatically
import java.awt.Window;
import java.lang.reflect.Method;
import javax.swing.JFrame;
@SuppressWarnings("serial")
class Fullscreen extends JFrame {
public Fullscreen() {
enableOSXFullscreen(this);
setVisible(true);
}
@SuppressWarnings({"unchecked", "rawtypes"})
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 (Exception e) {
System.out.println(e.getMessage());
}
}
@SuppressWarnings({"unchecked", "rawtypes"})
public static void requestToggleFullScreen(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 (Exception e) {
System.out.println("An exception occurred while trying to toggle full screen mode");
}
}
public static void main(String[] args) {
Fullscreen fs = new Fullscreen();
Fullscreen.requestToggleFullScreen(fs);
try {
Thread.sleep(5000);
} catch (InterruptedException e) {}
Fullscreen.requestToggleFullScreen(fs);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment