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
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

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
Welwyn Garden City, Hertfordshire, United Kingdom | Tesco.com
CMS Engineer - Welwyn Garden CityWho's behind the world's most successful online retailer? Just over 10 years ago we started Tesco.com (aka Dotcom). Today, we've an incredible 750,000 active customers and sales at just under ... more >
Welwyn Garden City, Hertfordshire, United Kingdom | Tesco.com
Technical Specialist Infrastructure - Welwyn Garden City Who's behind the world's most successful online retailer? Just over 10 years ago we started Tesco.com (aka Dotcom). Today, we've an incredible 750,000 active customers and sales at ... more >
Aylesbury, Buckinghamshire, United Kingdom | Grass Roots
SQL Database Administrator - Aylesbury - £DOEGrass Roots are one of the Sunday Times Top 100 companies to work for (2007 and 2008). Established in 1980, we're part of the Grass Roots Group, which is ... more >
Central London, United Kingdom | Royal Academy of Music
Head of Technology - London - Competitive salary & benefits The Head of Technology will lead and direct the Academy's Technology department, working with Senior Management to define and implement the IT strategy. The postholder ... more >
More job opportunities