Using Mac OS X System Images in Java

Here’s a Java feature in Leopard: You can use the system images, found on Apple toolbars and applications, in Java. They exist in a virtual file system called “NSImage”. Here’s an example to show the image for “Icon View”.

import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class ScratchSpace {

public static void main(String[] args) {
JFrame frame = new JFrame();
final Container pane = frame.getContentPane();
pane.setLayout(new FlowLayout());

/***********/
Image image = Toolkit.getDefaultToolkit().getImage(“NSImage://NSIconViewTemplate”);
pane.add(new JLabel(“Icon”, new ImageIcon(image), SwingConstants.LEFT));
/***********/

frame.pack();

frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.setVisible(true);
}
}

The full list of system icons can be found here. Scroll down the page to see examples of each icon.

Alas, this doesn’t work on Tiger. To get around this, you can use Leopard to create a copy of the image as a PNG. Then you can use the PNG. This code creates a PNG file for a specified image template.

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.IOException;

public class ScratchSpace {

public static void main(String[] args) throws IOException {
writeImage(“NSListViewTemplate”);
}

private static void writeImage(String imageName) throws IOException {
Image image = Toolkit.getDefaultToolkit().getImage(“NSImage://” + imageName);
ImageIO.write((RenderedImage) image, “png”, new File(imageName + “.png”));
}
}