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

Free email newsletters




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
Guildford, Surrey, United Kingdom | Enstar
 IT Development Manager/IT Development Project manager - Guildford - £40k - £60k plus benefits   Enstar (EU) Limited (formerly Castlewood (EU) Limited) is seeking an IT Development Project Manager and an IT Development Manager to ... more >
London, Haringey, United Kingdom | Haringey Council
PMO Support Officer - Haringey, London - £32,289 - £37,542 pa   Experienced project support officer required by the internal IT services organisation of a London borough council to work within its Programme Management Office ... more >
Leek Wootton, United Kingdom | Warwickshire Police
 IT Business Analyst - Leek Wootton, Warwickshire - £29,112 - £31,491 PA - 37 hrs per week   Everyone who works for Warwickshire Police helps to protect our communities from harm. Work with us and ... more >
Oxford, Oxfordshire, United Kingdom | University of Oxford
Senior Business Analyst - Oxford University - £34,793 - £45,397   Business Services & Projects (BSP) Are you an experienced Business Analyst with the skills to improve the efficiency of Oxford University's business systems? The ... more >
More job opportunities