Retired Document
Important: This document may not represent best practices for current development. Links to downloads and other resources may no longer be valid.
Developing a Swing Application
This chapter covers the steps needed to develop Swing applications. First, this chapter guides you through the creation of a simple application, completely based on Project Builder’s Swing project template. Second, to show how to port an existing Swing application to Mac OS X, it shows how to create a Swing project based on Sun’s File Chooser Demo application and deploy it as a Mac OS X application; the finished project is in companion/projects/FileChooser
. (See Introduction to Project Builder for Java for details on this document’s companion files.) Finally, this chapter explains how to change the icon the Finder displays for the application from the generic Java application icon. .
Creating the “Hello, Swing” Application
The Swing application template provides another version of the “Hello, World” application. Follow these steps to create a project that demonstrates how a Swing application looks in Mac OS X.
Launch Project Builder. It’s located in
/Developer/Applications
.Create a Java Swing application project.
Choose File > New Project, and select Java Swing Application under Java in the project-template list of the New Project pane.
Name the project and choose a location for it.
In the New Java Swing Application pane of the Assistant enter
Hello_Swing
in the Project Name text input field, click Choose, and choose a location for the project folder.
When done, you should see the Project Builder window, shown in Figure 4-1. The product, Hello_Swing.app
, appears in red because it hasn’t been built.

Build and run the application by choosing Build > Build and Run. Figure 4-2 shows the running Hello_Swing application.

Creating the File Chooser Demo
This section explains how to use existing Java source files to create a Swing-based Mac OS X application.
You can download source code that demonstrates the use of the JFileChooser class (javax.swing
) at http://java.sun.com/docs/books/tutorial/uiswing/components/filechooser.html. You can also use the files included with this document in companion/source/FileChooser
(see Introduction to Project Builder for Java for details on companion files).
Perform these steps to create a file-chooser demonstration project.
Create a Java Swing application project named
FileChooser
.Remove the standard source files from the project:
Add the source files and image files required for the project:
Choose Project > Add Files.
Navigate to where the source files reside, select them, and click Add. Figure 4-4 exemplifies the addition of the file-chooser demonstration files in
companion/source/FileChooser
.Figure 4-4 Adding source files to a project in Project Builder In the dialog that appears, select “Copy items into destination group’s folder” and make sure the FileChooser target is selected in the Add To Targets list.
Repeat the previous step for the image files.
Examine the FileChooser target to verify that the newly added files are assigned to the correct build phases:
Click the Targets tab and select the FileChooser target in the Targets list.
Select the Sources build phase and the Java Resource Files build phase in the target editor. Make sure the source files and image files you added to the project appear in the Sources pane and the Java Resource Files pane, respectively.
Change the name of the main class in the information property list:
Select Pure Java–Specific under Simple view under Info.plist Entries in the target editor.
Enter
FileChooserDemo
in the Main Class text field.
Clean the FileChooser target by choosing Build > Clean and click Clean Active Target in the dialog that appears.
Cleaning the target erases any temporary files stored in the target’s
build
directory, which may have been left there in previous builds. (If you didn’t build the application, you may skip this step.)
Build and run the application. You should see the window shown in Figure 4-5.

If instead of a running application you get an error message like the following in Project Builder’s Run pane, make sure that the name of the application’s main class matches the contents of the Main Class entry of the Pure Java–Specific pane of the Info.plist Entries pane in the target editor.
[LaunchRunner Error] The main class "FileChooser" could not be found. |
[JavaAppLauncher Error] CallStaticVoidMethod() threw an exception |
java.lang.NullPointerException |
at apple.launcher.LaunchRunner.run(LaunchRunner.java:85) |
at apple.launcher.LaunchRunner.callMain(LaunchRunner.java:50) |
at apple.launcher.JavaApplicationLauncher.launch(JavaApplicationLauncher.java:52) |
Exception in thread "main" |
FileChooser has exited with status 0. |
When you click the Show FileChooser button of the FileChooserDemo window, you should see a window like the one in Figure 4-6. Of course, the actual look of the window depends on the selections you make in the FileChooserDemo window.

Changing an Application’s Icon
The Resources folder of an application package holds several types of files, including icon files. The Finder consults the CFBundleIconFile
information property list entry to determine which of these files to use as the application’s icon.
Follow these steps to change the icon of the FileChooser application developed in Creating the File Chooser Demo from the default icon.
Identify the icon file for the new icon.
You can find an icon file in
/Developer/Applications/Pixie.app/Contents/Resources/Big.icns
.In Terminal, execute the following command:
cp /Developer/Applications/Pixie.app/Contents/Resources/Big.icns <FileChooser_project_directory>
Remove the
FileChooser.icns
file from the FileChooser project:Select
FileChooser.icns
in the Files list in the Project Builder main window.Choose Edit > Delete or press the Delete key.
Click Delete References & Files.
Add the icon file for the desired icon to the project:
Choose Project > Add Files.
Select
Big.icns
in the file list and click Add.In the dialog that appears, make sure “Copy items into destination group’s folder” is not selected and click Add.
Make sure that the new icon file is assigned to the Bundle Resources build phase and not the Java Resource Files build phase.
Select the Java Resource Files build phase and the Bundle Resources build phase in the target editor of the FileChooser target.
Drag
Big.icns
from the Files list of the Java Resource Files pane to the Files list of the Bundle Resources pane.
Set the name of the icon file of the application.
Select Application Icon under Simple View under Info.plist Entries in the target editor.
Enter
Big.icns
in the “Icon file” text field of the Application Icon pane.
Clean the project, and build and run the application. The icon for FileChooser.app
in the build folder of the project should have the icon used by Pixie.

Copyright © 2003 Apple Computer, Inc. All Rights Reserved. Terms of Use | Privacy Policy | Updated: 2003-10-10