Update: I've taken the approach shown in this article and turned it into a much more robust Mac OS X app, which I'm selling for a whopping $0.99.
Follow this link for a free trial of my "Mac OS X Hide Your Desktop Icons" application.
I wrote a very small "Java on Mac OS X" application that I'd like to share here today. This one is a little different, so let me explain the problem.
I've found that using Mac OS X is different from using Windows, in that with Mac applications, I almost never use them in full-screen mode. Typically they occupy 1/2 or 2/3 of my over screen, and then I have to see my Mac Desktop behind the application window. I actually prefer everything about this approach except for one thing: My Desktop is usually very cluttered, and I don't like to see that clutter while I'm working.
Given that background, I created an application this morning that I call DesktopShield. It is just a full-screen, black JFrame, and when I position it behind my text editor, it looks like this:
(You can click through that image to see a full-screen image.)
As you can see, when I'm editing an article, like this one, all I have to see now is my editor and a black background. There's no desktop clutter in the background, just me and my typing.
The Java source code
The source code for this application is very simple. I've made it harder than it needs to be (because I pulled it out of another, similar application), and it consists of the following three classes.
DesktopShield.java This class contains the main
method for the application, and is essentially the "controller" for the application. It sets up some Mac-specific things, can handle the Application "quit" event, and launches the JFrame.
MacAdapter.java This class extends the Apple ApplicationAdapter class. It's only purpose in this application is to handle the Mac "quit" event.
DesktopShieldFrame.java This class extends the JFrame class, and just sets the properties on the JFrame that I want.
For this simple application I could easily combine these three files into one main class, but I find it's best to keep things organized, and I also want to expand on this basic idea.
Here's the source code for these classes. First, the DesktopShield.java source code:
package com.devdaily.desktopshield; import javax.swing.JFrame; import javax.swing.SwingUtilities; import com.apple.eawt.Application; import com.apple.eawt.ApplicationEvent; /** * DesktopShield.java * Copyright 2010, Alvin J. Alexander, devdaily.com. * * This is a simple application that displays a black JFrame that occupies the * entire screen. While this isn't interesting by itself, I use it as a * "screen" or "shield" that hides my Desktop while I'm working in another * application, such as a text editor. By not seeing all the clutter on * my desktop, this helps me focus on the text editor. * * This file is part of my "DesktopShield" application. * * The DesktopShield application is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * The DesktopShield application is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with the DesktopShield application. If not, see <http://www.gnu.org/licenses/>. * */ public class DesktopShield { private JFrame mainFrame; private static final String APP_NAME = "DesktopShield"; public static void main(String[] args) { new DesktopShield(); } public DesktopShield() { // set some mac-specific properties System.setProperty("apple.awt.graphics.EnableQ2DX", "true"); System.setProperty("apple.laf.useScreenMenuBar", "true"); System.setProperty("com.apple.mrj.application.apple.menu.about.name", APP_NAME); // create an instance of the Mac Application class, so i can handle the // mac quit event with the Mac ApplicationAdapter Application macApplication = Application.getApplication(); MacAdapter macAdapter = new MacAdapter(this); macApplication.addApplicationListener(macAdapter); // display the jframe SwingUtilities.invokeLater(new Runnable() { public void run() { mainFrame = new DesktopShieldFrame(); mainFrame.pack(); mainFrame.setVisible(true); } }); } public void handleQuitEvent(ApplicationEvent e) { System.exit(0); } }
Next, the MacAdapter.java source code:
package com.devdaily.desktopshield; import com.apple.eawt.ApplicationAdapter; import com.apple.eawt.ApplicationEvent; /** * MacAdapter.java * Copyright 2010, Alvin J. Alexander, devdaily.com. * * This file is part of the DesktopShield application. This class implements the * Apple/Mac/Java ApplicationAdapter class, specifically the handleQuit * method of that class, to help shut down this application properly. * * The DesktopShield application is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * The DesktopShield application is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with the DesktopShield application. If not, see <http://www.gnu.org/licenses/>. */ public class MacAdapter extends ApplicationAdapter { private DesktopShield handler; public MacAdapter(DesktopShield handler) { this.handler = handler; } public void handleQuit(ApplicationEvent e) { handler.handleQuitEvent(e); } }
And finally, here is the DesktopShieldFrame.java source code:
package com.devdaily.desktopshield; import java.awt.Color; import java.awt.Toolkit; import javax.swing.JFrame; /** * DesktopShieldFrame.java * Copyright 2010, Alvin J. Alexander, devdaily.com. * * This file is part of the DesktopShield application. It creates * a simple full-screen black JFrame, with no adornments on the * JFrame. * * The DesktopShield application is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * The DesktopShield application is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with the DesktopShield application. If not, see <http://www.gnu.org/licenses/>. */ public class DesktopShieldFrame extends JFrame { public DesktopShieldFrame() { // no adornments on the mainframe this.setUndecorated(true); this.setResizable(false); // all black this.getContentPane().setBackground(Color.black); // fill the screen setPreferredSize(Toolkit.getDefaultToolkit().getScreenSize()); setMinimumSize(Toolkit.getDefaultToolkit().getScreenSize()); } }
A native Mac application
If anyone is interested, I also have an Ant build file that I use to turn this Java code into a bundle that looks and works just like a native Mac OS X application. (I now have this app installed on my Mac Dock, just like any other Mac application.) Just drop me a line, or leave a comment below, and I'll be glad to make that available as well.