About RSS
Search for: in 
Image: Netbeans
The Project Wizard can help you start a new project in Netbeans
R E L A T E D   C O N T E N T
Jargon Buster

ADVERTISEMENT

Hands on: Building Java apps with Swing

The GUI builder in Netbeans 5.x makes light work of creating desktop applications

Tim Anderson, Personal Computer World 05 Oct 2006
ADVERTISEMENT

The days when Java was just not good enough for graphical desktop applications are long gone.

Applications built with Swing, the Java GUI (graphical user interface) library, once had a reputation for slow performance and ugly appearance, but that’s not the case today.

It is true that Java applications are resource-hungry, but that is also the case with Microsoft’s .Net framework. If you want efficiency, native code still wins out by a large margin.

There is another factor in Java’s relative lack of popularity on the desktop, which is that creating a Java user interface is more challenging than it is in Visual Studio or Delphi.

There are a couple of reasons for this. First, Java uses layout managers to arrange widgets on a form, which is great for smooth-scaling on different platforms, but awkward when you need a quick result.

Second, the Swing library implements the Model-View-Controller architecture, which is good for more complex projects, but harder to learn.

The good news is that the latest versions of Netbeans, the Sun-sponsored free Java IDE (Integrated Development Environment), has a superb GUI builder called Matisse.

It uses a new library called Swing Layout Extensions library, which is better suited to the needs of a visual designer.

The Netbeans team also improved the usability of the IDE. Overall, Netbeans is now the best tool if you need to put together a quick Java GUI application.

Here is a step-by-step guide to creating the famous to-do list example with Netbeans 5.x.

A Netbeans to-do list
Start a new Java application in Netbeans, using the project wizard to name it PCWNetBeansExample.

Check the option to create a Main class, which is the entry point for the application, and click Finish. Next, right-click the package in the project explorer and choose New JFrame Form.

Call the new class MainForm, click Finish, and it will open in the Matisse GUI designer.

If you try to run the application now, no form will appear. To make it work, edit the main method in Main.java as follows:
MainForm f = new MainForm();
f.setVisible(true);

Now you can run the application and see a blank form, just like a new Visual Basic or Delphi project.

When you close the form, the application quits, because Netbeans automatically sets the JFrame’s defaultCloseOperation to EXIT_ON_CLOSE.

The next step is to lay out the form. Unlike most Java GUI builders, you don’t need to worry about layout managers.

Simply place a JList, a JLabel, a JTextField and two JButtons, aligning them using the form designer’s snap-to lines.

When you place the JList, Netbeans automatically places a JScrollPane as well, which is almost always what you want.

The GUI builder also makes commonly used properties easy to find. For example, right-click the first JButton, and choose Change Variable Name. This button will add an item to the list, so call it btnAdd.

Right-click again and choose Edit text, changing it to Add.

Right-click the JLabel and edit the text to say ‘New item’, noticing how other widgets shift right to accommodate the longer text. Using these techniques, call the JList lstItems, and the JTextField txtNewItem.

One complication of a JList is that its data is handled by a separate class implementing the ListModel interface.

Netbeans creates an instance of AbstractListModel automatically, but a DefaultListModel is more convenient.

Click the Source view and add the code:
import javax.swing.*;
below the package statement but above the class declaration. Then add:
private DefaultListModel dlm = new DefaultListModel();
in the body of the MainForm class.

Finally, view the properties of the JList, find the model property, and click the button to open the model editor.

Remove the existing model, and from the Select Mode button, choose Form Connection.

Select User Code and type dlm in the box. If you then look at the source, Netbeans has generated the following line:
lstItems.setModel(dlm);

This code is in a guarded section, which means you cannot type over it directly.

You will find many similar code snippet editors in Netbeans, allowing flexible customisation without losing the convenience of working through property editors.

Tags: Visual Programming

Like this story? Spread the news by clicking below:

Post this to Delicious del.icio.us    Post this to Digg Digg this    Post this to reddit reddit!

Permalink for this story
R E A D E R   C O M M E N T S

M A R K E T P L A C E
Sponsored links
F E A T U R E D   J O B S
| Randstad Technologies
Project Manager required to join fast-growing IT software house in the Bristol area. We are looking for candidates with strong Project Management skills looking to better their skills with a market-leader offering long-term progression. The ... more >
| Computer People
My client is looking for an ETL Developer to identify, prioritise and develop new ETL packages and maintaining existing packages. Delivering, maintaining and testing ETL solutions. Investigating new technology platforms and technologies. Documentation of ETL ... more >
| Computer People
Computer People are currently recruiting for a large and rapidly expanding IT Services company that are looking to add to it’s talented ITIL Change Management function within their Central Service Center based in Milton Keynes. ... more >
| Computer People
Web amp; SQL, Crystal Reports, .NET (C# amp; reputation in the Market. They seek a creative Developer with PHP experience to join their team. We are looking for someone who has a strong understanding of ... more >
More job opportunities