Transparent JFrame - How to make a JFrame transparent on Mac OS X

Summary: How to make a Java JFrame transparent (translucent) on Mac OS X.

A lot of people complain about a lot of things in regards to Java on Apple's Mac OS X platform, and okay, occasionally I'm one of them, but a very cool thing you can do on OS X is to create translucent (transparent) frames and windows with Java.

As an example of this, today I'm going to share some Java Swing source code where I create a translucent JFrame. To make it a little more interesting/real, I've added a a JTextArea and JScrollPane to show you how it works, and how it looks.

Create a transparent JFrame with one magic line

Actually, creating a transparent JFrame on Mac OS X is no big deal, at least not as long as you're using the right software versions. The only line of code you really need is shown here:

editorFrame.getRootPane().putClientProperty("Window.alpha", new Float(0.8f));

This special Window.alpha property -- which is Mac OS X specific -- lets you set the translucency level of your JFrame or JWindow. If you're not shipping your product to other users on a variety of other systems where you need to check the operating system and revision level, this is all you have to do.

You can set this Window.alpha property anywhere between 0.0 and 1.0, with the lowest values making your window almost invisible. I'll show the effect of different settings shortly.

My Java translucent JFrame source code

I've written an example Java/Swing application to demonstrate this JFrame transparency effect on Mac OS X, and I'm sharing the source code here. Most of the code is boilerplate Java Swing code, but here's a quick description of it:

  1. I create a JFrame object named editorFrame.
  2. I create a JTextArea, place that in a JScrollPane, and place that in the center panel of the JFrame's default BorderLayout.
  3. I set the Window.alpha setting, as shown above.
  4. I center the JFrame, and then make it visible.

Given that introduction, here's my sample Java code:

package com.devdaily.swingtests.transparency;

import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.Dimension;

/**
 * Creates a translucent frame (jframe) on Mac OS X.
 * @author alvin alexander, devdaily.com
 */
public class MacTranslucentFrame
{
  public static void main(String[] args)
  {
    new MacTranslucentFrame();
  }

  public MacTranslucentFrame()
  {
    SwingUtilities.invokeLater(new Runnable()
    {
      public void run()
      {
        JFrame editorFrame;
        editorFrame = new JFrame("Java Mac OS X Translucency Demo");
        editorFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        
        // this is what sets the transparency/translucency on Mac OS X
        editorFrame.getRootPane().putClientProperty("Window.alpha", new Float(0.8f));
        
        // create and add a scroll pane and text area
        JTextArea textArea = new JTextArea(5, 30);
        JScrollPane scrollPane = new JScrollPane(textArea);
        textArea.setText("Hello, world");
        scrollPane.setPreferredSize(new Dimension(300, 185));
        editorFrame.getContentPane().add(scrollPane, BorderLayout.CENTER);

        editorFrame.pack();
        editorFrame.setLocationRelativeTo(null);
        editorFrame.setVisible(true);
      }
    });
  } 
}

Sample screenshots

Here are a few sample screenshots of this application running. I set the translucency at three different levels for these shots: 0.8f, 0.6f, and 0.4f. The application is running in front of a blue background, with a folder intentionally placed behind the JFrame.

Here's the translucency with a setting of 0.8f:

Here's roughly the same screen shot with a translucency with a setting of 0.6f:

And finally here's roughly the same screen shot with a translucency with a setting of 0.4f:

This is a very cool effect for Java Swing applications on the Mac platform. I've written my own custom editor that I use on the Mac, and this is one of those nice effects that gives an application a little extra "something" that customers appreciate.