In this recipe, you will see how to create a new Java Desktop Application project. You will have to understand the component Palette, which classes are created by the IDE and what they represent for your application, and how to use the inspector to see which components are in your project.
The following screenshot shows how the third step of the wizard should look like:
Creating a Desktop application presents us with new options. The Choose Application Shell gives two:
When one of these options is selected, NetBeans provides the user with some explanation of what files are being created and what to expect from them. This explanation can be seen at the right-hand side of the Application Shell. After following these steps, NetBeans will create a complete project structure that should look like this:
Three Java files are created as a skeleton, so that the developer can start coding on top of them to create an application. To create the applications, NetBeans follows the Swing Applications Framework. It is important to understand what each file does so we can implement them correctly:
DesktopAboutBox.java:
As seen in most Windows applications. It is implemented to give some information about the company, developer, and the version of application.DesktopApp.java:
The application launcher. Basically it is used to initialize resources, inject them into the desktop app, and run the GUI class, in our case, DesktopView. This is where the main method exists.DesktopView.java:
Our main graphical class. This is where we will lay out the components, program those components to react to events, and so on.Note that these classes have different icons. DesktopApp.java
has an icon corresponding to a normal Java class, since it is a normal Java class with a main method. The "Graphical" classes are seen with a representation of a small window inside the icon.
NetBeans creates these classes so it can better manage the design and placement of components in the Java files. This is achieved by the addition of a block of non-editable code, which is highlighted blue to differentiate it from the rest of the production code. This non-editable block is created by NetBeans as a blueprint for the entire application so the basic functionality will always be the same. If modification of this non-editable block were to be allowed, the application could perform in ways that could not be determined by the IDE.
It is possible to see this when toggling between Design and Source in the Java editor, as shown in the next screenshot:
After selecting the design button, we have three small buttons available:
|
Selection mode: Let's the developer choose the components that are inside the Form. |
|
Connection Mode: Launches a wizard that helps the developer to set an event that will establish a connection between objects. |
|
Preview Design: Launches a preview of the GUI that is being written. |
The other subsequent buttons are used for component alignment and resizability.
By default, the main graphical class, in our case DesktopView.java
, is already created with a few components:
The components range from Panels and menus to a status bar. The component Tree View, in the previous screenshot, shows where each component belongs. In our case, Form DesktopView has a mainPanel [JPanel] and a menuBar [JMenuBar]. The menuBar then has a fileMenu [JMenu] and a helpMenu [JMenu], and so on. This kind of organization can have multiple levels in the hierarchy, and different components belonging to each level.
Form DesktopView is the main window where all of our components will be placed, or inside one of the child components.
From the component Palette, it is possible to drag-and-drop the components into Form under the GUI Designer.
There are a number of toolkits available, such as Swing, AWT, and Persistence components. Each of these frameworks has its own separate tab. It is also possible to add components from external frameworks to our example, though we'll only use Swing Application Framework.
Under Palette, the Properties window displays Properties, Binding, Events, and Code related to the selected component in the Designing window:
Interested in seeing a cool Java Desktop project? It is not necessary to run the project to see the design changes in the application; check out how, in the following section.
Of the sample projects available, the Mars Rover Viewer is the coolest and most complete Java Desktop Application, since it shows many concepts related to Swing Application Framework, background tasks, and resource maps, and is included with the IDE.
The contents of the Mars Rover Viewer will be shown in the Editor. After this, run the main project either by using the shortcut F6 or by clicking on the Run Main Project button on the toolbar.
3.144.110.32