With NetBeans 8.0, creating a desktop application is very similar to creating a standard console application as shown in Chapter 1, Using NetBeans Projects. To create a Swing application, we need to first create a blank Java application and then add a JFrame derived class to the application that will act as the main frame for the application.
Once we've created a frame, we can set its title and give it an icon and we will get a basic Java desktop application.
Let's see how that can be achieved.
For more information about JFrames and how to use top-level containers, check out http://docs.oracle.com/javase/tutorial/uiswing/
components/toplevel.html.
Getting ready
To complete this recipe, we'll be using the Java SE NetBeans IDE download bundle.
The Java EE or All bundles could also be used, but we will not be using any of their features in this recipe.
How to do it…
First off, we'll create a blank Java application that we can add our frame into with the following steps:
1. Click on File and then New Project….
2. On the resultant dialog, select the Java category and Java Application as the project.
3. Click on Next.
4. Enter the Project Name field as JarViewer.
5. Enter the Project Location field. (The default location will most likely be correct.) 6. Ensure Create Main Class is checked and enter com.davidsalter.cookbook.
jarviewer.Main as the Main Class name, as shown in the following screenshot:
7. Click on Finish.
NetBeans has now created a blank application with a single main class in it.
Let's create a GUI frame for our application and open it when the application is launched with the following steps:
1. Right-click on the Source Packages node in the Projects explorer and select New and then JFrame Form….
2. Enter the Class Name field as MainFrame.
3. Enter the Package field as com.davidsalter.cookbook.jarviewer.gui, as shown in the following screenshot:
4. Click on Finish.
NetBeans will now create a MainFrame class for us and open it for GUI editing as shown in the following screenshot:
In the center of the screen, we can see that a blank form has been created with no components on it. To design GUIs, we drag components from the Palette section at the right-hand side of the main window onto the form, laying them out in the design we want.
We'll look at adding controls to a form in a later recipe. For now, let's set our application's title and give it an icon so that it starts to look like a proper GUI application, using the following steps:
1. Click on the MainFrame.java design surface in the center of the NetBeans IDE window. The [JFrame] - Properties explorer should now be displayed.
2. Locate the title property and enter Jar File Viewer, as shown in the following screenshot:
3. From the assets folder of the code download bundle for this chapter, locate the folder_explore.png file and drag it onto the com.davidsalter.cookbook.
jarviewer.gui package in the Projects explorer. This will add the file into the NetBeans project so that we can reference it from within our application. The Projects explorer for our project is shown in the following screenshot:
4. Click on the design surface for the MainFrame.java class again and locate the iconImage property.
5. Click on the … button ( ) for the iconImage property to open up the property editor window.
6. Select the Set Form's iconImage property using field as Custom code.
7. Enter the custom code within the Form.setIconImage edit as:
new javax.swing.ImageIcon(getClass().getResource ("/com/davidsalter/cookbook/jarviewer/gui/
folder_explore.png" )).getImage()
The [JFrame] - iconImage window is shown in the following screenshot:
8. Click on the OK button to set the iconImage property.
We've now created the basics for our desktop application. The final step is to wire up the Main class to instantiate the frame when the application is executed.
9. Double-click on Main.java within the Projects explorer to open it for editing.
10. Change the main method within Main.java to read:
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() { public void run() {
new MainFrame().setVisible(true);
} });
}
11. Press F6 to run the application.
When creating a JFrame class, NetBeans actually creates a main method within the JFrame class; so, there is no need to create a separate Java class to implement the main method. We have created a separate Main class within this recipe to show how to display a JFrame within an application.
How it works…
In this recipe, we've created the first of our Swing components—a MainFrame.java class that extends JFrame.
When this file is opened within NetBeans, it has three views:
f Source
f Design
f History
These three views are displayed in the following screenshot:
The Source view allows us to see the Java source code that the class is composed of.
This code is a mixture of automatically generated code and custom code that has been added by the developer.
The NetBeans GUI designer creates all the code for us to instantiate a form and initialize all the components on it, including their layout, size, and other properties. Code to use the basic properties that we entered within the Properties window (such as the frame's title) is also automatically generated for us. Any custom code that we enter for properties (such as the frame's iconImage property) is automatically added into the generated code.
When we look at the code for a GUI component, we can see that sections of the code are in editor folds that have a grey background. These sections of code are the ones that are automatically generated by NetBeans and cannot be edited within the Java editor. It's very important not to try to edit these sections of code in an editor external to NetBeans as this will almost always stop NetBeans from subsequently being able to edit the GUI component graphically.
If you want to edit the custom code that you have added as a component's property, then you need to select the custom code editor from the Properties window rather than attempting to edit the source code directly. NetBeans will not allow the custom code to be directly edited within the Java editor.
The Design view shows the layout of all the components on the frame. We will see more of this in the next recipe. For now, we'll just note that this is the area where we drag controls to build up our GUI.
The History view shows all the changes that we have made to the component, similar to the history we may see when viewing the history of a file within a source code control system such as Git or Subversion. Using the History view, we can revert to the earlier changes we made to the design of a form.
There's more...
When creating a JFrame form within a project, NetBeans automatically adds a main method into the new class. If your application only has one JFrame within it and you're not likely to do any other processing at application startup, then this can be a convenient way to automatically create a main() method for your application.
For each Java GUI class created by NetBeans, a corresponding XML file will be created that has the .form extension. In this recipe, we created a Java class called MainFrame.java. NetBeans automatically created a file called MainFrame.form in the same directory on disk to go along with this file. NetBeans stores all of its internal data within this .form file;
so, it's very important not to delete or edit these files manually. Don't worry though, when you distribute an application, these .form files do not need to be distributed with the application.
They are only required while developing within NetBeans.