Class MacIntegration

java.lang.Object
nl.colorize.util.swing.MacIntegration

public final class MacIntegration extends Object
Utility class for making macOS specific behavior available to Swing applications. This includes interaction with the application menu, the dock, fullscreen mode, and the notification center.

Which features are available depend on both the version of macOS and the JRE. In older versions of the JRE these features were accessed through the Apple Java extensions. As of Java 9 these extensions are no longer part of the JRE, with the functionality being added to the AWT desktop API. This class abstracts over these differences, using the appropriate API depending on the used JRE.

Some features were only introduced in later version of macOS, and are not available in earlier versions. Attempting to use a feature that is not available in the current macOS version will have no effect, rather than throwing an exception. This is done to prevent application code from being littered with feature detection checks. The same applies to attempting to use this class on a platform other than macOS, in which case all features will silently fail.

  • Field Details

  • Method Details

    • enableApplicationMenuBar

      protected static void enableApplicationMenuBar()
      Enables the application menu bar for Swing applications. By default, Swing will show separate menu bars for each window. This approach is common on Windows and Linux, but macOS applications generally use the same menu bar for the entire application.

      This method is called by SwingUtils.initializeSwing(), meaning there is normally no reason for calling this method from application code.

    • augmentLookAndFeel

      protected static void augmentLookAndFeel()
      Augments the Swing look-and-feel to look more like native macOS applications. This includes different look-and-feels for different versions of macOS.
    • setApplicationMenuListener

      public static void setApplicationMenuListener(ApplicationMenuListener listener)
      Registers a listener that will be notified every time a menu item in the macOS application menu is clicked.
    • setDockIcon

      public static void setDockIcon(Image icon)
      Changes the application's dock icon to the specified image.
    • setDockBadge

      public static void setDockBadge(String badge)
      Adds a badge to the application's dock icon with the specified text. Use null as argument to remove the badge.
    • bounceDockIcon

      public static void bounceDockIcon()
      Notifies the user of an event by bouncing the application's dock icon.
    • showNotification

      public static void showNotification(String title, String message)
      Sends a notification to the macOS Notification Center.

      Integration with Notification Center is only allowed if the JVM was started from inside an application bundle. Notifications sent from other locations will not be shown.

    • openBrowser

      @Deprecated public static void openBrowser(String url)
      Deprecated.
      Use SwingUtils.openBrowser(String) instead, which provides the same behavior but uses a cross-platform API that also supports other operating systems.
      Opens the default browser with the specified URL.
    • openFile

      @Deprecated public static boolean openFile(File file)
      Deprecated.
      Use SwingUtils.openFile(File) instead, which provides the same behavior but uses a cross-platform API that also supports other operating systems.
      Opens the default application for the specified file. Returns true if the file was successfully opened.
    • revealInFinder

      public static void revealInFinder(File file)
      Reveals the specified file or directory in the Finder. If the provided file is not a directory, the Finder will be opened for its parent directory, with the requested file selected.
    • goFullScreen

      public static void goFullScreen(JFrame window)
      Enables fullscreen mode for the specified window. This has the same effect as the user clicking the green window button.

      The approach to enable fullscreen mode depends on the Java runtime. If the Apple Java extensions are available, this will trigger Mac OS's "native" fullscreen mode. If the Apple Java extensions are not available, this behavior will be emulated by changing the window dimensions. Mac OS native fullscreen mode cannot be triggered with the standard library, see https://bugs.openjdk.java.net/browse/JDK-8228638 for details.

    • usesMetalRenderer

      public static boolean usesMetalRenderer()
      Returns true if a Swing or Java2D application is using the Metal renderer introduced in Java 17, and false if it is using the OpenGL renderer.