Image: Netbeans
The Project Wizard can help you start a new project in Netbeans

Hands on: Building Java apps with Swing

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

Written by Tim Anderson

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.

reader comments

related articles

 

related whitepapers

today's top stories

Learning from the credit crunch to avoid a broadband crunch

While it might be the most pressing issue de jour , the financial system isn’t the only area where government needs to... 10 Oct 2008

How careerism can warp IT procurement

Many working in IT put their career interests before those of their employer when weighing up purchasing options 10 Oct 2008

City in pressing need of skilled IT matchmakers

With the financial services sector plunging ever deeper into an M&A maelstrom, IT leaders are having their systems integration skills and due diligence expertise tested as never before 09 Oct 2008

The definitive guide to software development

Five key trends and five best practice tips to help you improve your programming capabilities 09 Oct 2008

Computing podcast - IT implications of the banking crisis, and the FSA clamps down on IT security

We discuss the effect of shotgun mergers and acquisitions on financial services IT staff, and examine the industry regulator's plan to fine directors for information security breaches 09 Oct 2008

Advertisement

Newsletter signup

Sign up for our range of FREE newsletters:

Existing User

Newsletter user login:

Jobs

Related jobs

Job of the week

Job alerts

Sign up here

Find your next job


IT Salary Checker

Check salary here

Advertisement

White papers

Search white papers

Top categories

VPN, Extranet and Intranet Solutions

WAN/ LAN Solutions

Network Security

Interoperability-Connectivity

Grid/ Utility Computing

Latest poll

Would you apply for a job that was advertised on Facebook or a similar social networking site?

Would you apply for a job that was advertised on Facebook or a similar social networking site?

The government is using Facebook to recruit IT staff - would you apply to such an ad?

Previous poll results

Latest audio and video articles

programming codeVideo

The definitive guide to software development

Five key trends and five best practice tips to help you improve your programming capabilities 09 Oct 2008

Podcast imageAudio

Computing podcast - IT implications of the banking crisis, and the FSA clamps down on IT security

We discuss the effect of shotgun mergers and acquisitions on financial services IT staff, and examine the industry regulator's plan to fine directors for information security breaches 09 Oct 2008

Latest in-depth articles

Financial Services Authority buildingAnalysis

FSA threatens executives with fines

Senior management to be held accountable for security lapses at banks 09 Oct 2008

Comment

Broadband must be a spending priority

For the economic health of the nation, the government would do better to bankroll an optical fibre rollout rather than prop up profligate banks 09 Oct 2008

Advertisement

Primary Navigation