Creating a Java Desktop Application

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.

How to do it...

  1. Create a Java Desktop Application project; this can be achieved by clicking File and then New Project or by pressing Ctrl+Shift+N (shortcut).
  2. In the New Project window, choose Java on the Categories side, Java Desktop Application on the Projects side, then click Next.
  3. A disclaimer window will be shown, click Next.
  4. At Name and Location, click on Project Name and enter DesktopApp.
  5. At Choose Application Shell, select Basic Application.
  6. Tick the box on Use Dedicated Folder for Storing Libraries.
  7. We can now proceed by clicking Next and then Finish.

The following screenshot shows how the third step of the wizard should look like:

How to do it...

How it works...

Creating a Desktop application presents us with new options. The Choose Application Shell gives two:

  • Basic Application
  • Database Application

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:

How it works...

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:

How it works...

After selecting the design button, we have three small buttons available:

How it works...

Selection mode: Let's the developer choose the components that are inside the Form.

How it works...

Connection Mode: Launches a wizard that helps the developer to set an event that will establish a connection between objects.

How it works...

Preview Design: Launches a preview of the GUI that is being written.

The other subsequent buttons are used for component alignment and resizability.

How it works...

By default, the main graphical class, in our case DesktopView.java, is already created with a few components:

How it works...

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.

How it works...

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:

How it works...

There's more...

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.

Check the source for the Mars Rover Viewer

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.

  1. In the New Project window, expand Samples on the Categories side and select Java on the Projects side. Then select Mars Rover Viewer and click Next.
  2. Leave all the default settings marked and then click Finish.

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.

Preview Design

During development it is often neccessary, to check the behavior and design of the application. NetBeans grants a very convenient way to do this with the Preview Design button.

The launched window will present the coded events, like pressing a button to launch another window.

See also

This was just a general introduction on how to create Java Desktop Applications. So if you want to do something more productive with your application, it is recommended to please check the following recipes:

  • Creating a Frame
  • Creating a Panel
  • Adding Event Listeners
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.110.32