Getting to Know ABook

by John Zukowski

You've used NetBeans to analyze the source code for the Address Book application over the past couple of months. It is now time to actually look and see what the application can do. Since a database comes integrated with the 1.6 runtime, you don't have to configure any MySQL or ODBC target to get started. Just run the application. Download Address Book in .zip format.

Once you unpack the file, the ABook.jar file is found in dist subdirectory of the file. Run it with the -jar option to the java command:

  > java -jar ABook.jar

This will show a brief splash screen as shown in Figure 1:

Splash Screen
Figure 1. Splash Screen

Before going to the main application screen:

Main Application Screen
Figure 2. Main Application Screen

The image to use for the splash screen is specified by including a line in the manifest file:

  SplashScreen-Image: abook/images/abook-splash.gif

The application looks like a typical, though fairly basic, address book manager. You can add, edit, and delete contacts, as well as filter the contact list. You may also notice an icon on the status tray when the application is running.

Right clicking on the status tray icon will bring up its popup menu, with an Exit option on it. The code for this is found in the ContactSystemTray class. Here is what the key parts of the class looks like, setting up the tray and menu.

  SystemTray tray = SystemTray.getSystemTray();
  URL url = ContactList.class.getResource("images/tray.gif");
  Image image = Toolkit.getDefaultToolkit().getImage(url);

  ActionListener exitListener = new ActionListener() {
    public void actionPerformed(ActionEvent e) {
  PopupMenu popup = new PopupMenu();
  MenuItem defaultItem = new MenuItem("Exit");

  TrayIcon trayIcon = new TrayIcon(image, "Address Book", popup);

Pressing the New Contact button will bring up the "Email Contacts" screen, for entry of a new person with potentially three tabs worth of associated information, including a picture.

New Contact
Figure 3. E-mail Contacts

Pressing the Add button brings up a JFileDialog, prompting to enter a GIF, JPG, or PNG image, smaller than 64K. The preview panel only shows a small portion of the image, so if you're face shot happens to be too big, the image isn't scaled.

New Contact
Figure 4. Contact Photo

Saving of the image is done via a Blob column in the database. The getPicture() method of the Contact record returns a byte array, which is converted to a ByteArrayInputStream for storing as the blob.

  if(record.getPicture() != null){
    ByteArrayInputStream pictureByteStrm = 
        new ByteArrayInputStream(record.getPicture());
      pictureByteStrm, record.getPicture().length);
  } else {
    stmtUpdateExistingRecord.setBlob(18, null, 0);

If you're wondering where the database is stored between runs, the program keeps the database files in the .addressbook subdirectory of your home directory, the user.home system property. See the setDBSystemDir() method of the AddressBookDao and ContactDao classes for where that is configured:

    private void setDBSystemDir() {
        // decide on the db system directory
        String userHomeDir = System.getProperty("user.home", ".");
        String systemDir = userHomeDir + "/.addressbook";
        System.setProperty("derby.system.home", systemDir);
        // create the db system directory
        File fileSystemDir = new File(systemDir);

Derby is the name of the Apache project which provided the basis for the Java DB in Java SE 6.

One last feature worth highlighting is related to the email, website and notes fields. If you look at all the data for a specific contact, you'll notice these three fields are in bold on the data tab.

The Data Tab Figure 5. The Data Tab

The reason behind that is you can click on the data in the form and the application associated with the data will be launched. Click on the email address to start writing an email to the person. Clicking on the Website text will launch the browser, with the Notes text opening you're local text editor. Here's the ContactInfoPanel method code behind the mailing operation:

  private void openMailTo(String mailURI) {
      System.out.println("Class java.awt.Desktop is not supported on "
      + "current platform. Farther testing will not be performed");
    Desktop desktop = Desktop.getDesktop();
    if (!desktop.isSupported(Desktop.Action.MAIL)) {
      System.out.println("Desktop do not support the action of MAIL.");
     * launch the the mail composing window without a mailto URI.
    try {
      System.out.println("Testing desktop.mail()");
      URI  mailToURI = new URI("mailto:" + mailURI);
    } catch (IOException e) {
      System.err.println("EXCEPTION: " + e.getMessage());
    } catch (URISyntaxException uriEx) {
      System.err.println("URISyntaxException: " + uriEx.getMessage());

The method first checks to see if the Desktop concept is supported on your platform before checking if the MAIL action is supported. It then creates a mailto: string to eventually be used to start up your email client. Similar code is behind the other two operations.

There is much more going on here with the address book application. You'll be looking at more over the upcoming weeks. This was a quick tour of the application and helps you to understand some of the things revealed by the prior source code analysis. Do try out the application some more to get a better feel for its feature set.

Please join your fellow developers in the upcoming Sun Tech Days in Boston, which is free and yet technology-rich conference for world-wide developers. Please see sample list of topics below.

-JRuby on Rails
-Web 2.0 and Ajax Technologies: i.e. Dojo, jMaki, GWT
-JavaServer Faces and Ajax: DynaFaces
-Bean Binding in Swing
-Java SE 6
-Java Persistence API (JPA)
-Web Services Interoperability Technology (WSIT)
-Java FX
-Concurrency programming
-SVG for mobile devices
-Virtualization technology
-Java EE 6

The speakers are well-known software architects and engineers who will deliver technically in-depth yet practical contents with many demos. (You will be able to try the demo's yourself since they will be available in the form of downloadable hands-on labs.) You can also request 1-1 meetings with speakers.

Comments: is missing

Posted by Myriam Abramson on September 07, 2007 at 04:07 AM PDT #

So, sorry. I fixed the link to the Abook download. Thank you for letting me know! Dana

Posted by Dana Nourie on September 07, 2007 at 04:15 AM PDT #

Not anymore ... Thanks.

Posted by Myriam Abramson on September 07, 2007 at 04:16 AM PDT #

For those of us who may have just started. How can we go back to the find the beginning of the ABook NetBeans discussion?


Posted by Russel on November 12, 2007 at 12:44 AM PST #

The Abook articles started with:




You bring up a good point, and tomorrow I will go through and link these together so everyone can follow the flow.

Dana Nourie

Posted by Dana Nourie on November 13, 2007 at 01:12 AM PST #

i know the tutorial states that the derby.jar is include, but when imported into netbeans 6, i receive an error stating that access is denied to the properties of the file and cannot complete this tutorial.

My next step was to resolve the reference problem manually (by 'manually' pointing to the derby.jar file) and i still received the same error.

any ideas on how to resolve this issue?


ps - should i just download the derby distribution and run the DB server and connect that way? I dont want to have to go through the trouble if this so-called portable JavaDB is supposed to work when included w/ an application...

Posted by ray bielun on January 18, 2008 at 01:54 AM PST #

Post a Comment:
Comments are closed for this entry.

Java Technology Fundamentals are now covered as part of the Core Java Technology Tech Tips. Please go there for more tech tips.


« August 2016