Tech all over the world
Friday, August 31, 2007
  Java Technology Fundamentals (Aug '07)

You are receiving this email at kiri.tech@gmail.com because you have subscribed to the Java Technology Fundamentals newsletter. To update your communications preferences, please see the links at the bottom of this message. We respect your privacy and post our privacy policy prominently on our Web site: http://www.sun.com/privacy


August 2007

Welcome to the Java Technology Fundamentals Newsletter

This monthly newsletter provides a way for you to learn the basics of the Java programming language, discover new resources, and keep up-to-date on the latest additions to Sun Developer Network's New to Java Center.

The Java Technology Fundamentals Newsletter is now available in a blog format. Content will appear throughout the month and include extras.

Start reading the Java Technology Fundamentals Blog today!

Note : For the code in this issue of Fundamentals to compile, use the JDK 6 software.

In This Issue

» Basic Java Technology Programming
» Making Sense of the Java Platform Classes and Tools Annotations
» Desktop Java Platform Development
» Server-Side Java Platform Development
» Java Technology Training
» For More Information

 

  Basic Java Technology Programming

Getting to Know ABook

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 Abook.zip file. Run it with the -jar option to the java command:

  > java -jar ABook.jar

This will show a brief splash screen:

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) {
      System.out.println("Exiting...");
      System.exit(0);
    }
  };
            
  PopupMenu popup = new PopupMenu();
  MenuItem defaultItem = new MenuItem("Exit");
  defaultItem.addActionListener(exitListener);
  popup.add(defaultItem);

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

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

Contact Photo
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());
    stmtUpdateExistingRecord.setBlob(18, 
      pictureByteStrm, record.getPicture().length);
    pictureByteStrm.close();
  } 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);
        fileSystemDir.mkdir();
    }

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 TabFigure 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) {
    if(!Desktop.isDesktopSupported()){
      System.out.println("Class java.awt.Desktop is not supported on "
      + "current platform. Farther testing will not be performed");
      System.exit(0);
    }
        
    Desktop desktop = Desktop.getDesktop();
    if (!desktop.isSupported(Desktop.Action.MAIL)) {
      System.out.println("Desktop does not support the action of MAIL.");
      System.exit(0);
    }
        
    /*
     * launch the mail composing window without a mailto URI.
     */
    try {
      System.out.println("Testing desktop.mail()");
      URI  mailToURI = new URI("mailto:" + mailURI);
      desktop.mail(mailToURI);
    } catch (IOException e) {
      System.err.println("EXCEPTION: " + e.getMessage());
      e.printStackTrace();
    } catch (URISyntaxException uriEx) {
      System.err.println("URISyntaxException: " + uriEx.getMessage());
      uriEx.printStackTrace();
    }
        
  }

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.

  Making Sense of the Java Platform Classes and Tools

Data Transfer

The Swing toolkit supports the ability to transfer data between components within the same Java application, between different Java applications, and between Java and native applications. Data can be transferred via a drag and drop gesture, or via the clipboard using cut, copy, and paste.

Drag and Drop

Drag-and-drop support can be easily enabled for many of Swing's components (sometimes with a single line of code). For example, it's trivial to enable drag and drop and copy and paste support for JTable, Swing's table component. All you need to provide is the data representing the selection and how to get your data from the clipboard -- that's it!

Cut, Copy, and Paste

Most of the text-based components, such as editor pane and text field, support cut/copy and paste out of the box. Of course, menu items need to be created and "wired up" to the appropriate actions. Other components, such as list and tree, can support cut, copy, and paste with some minimal work.

PasswordStore supports data transfer in a variety of ways:

  • The text in both the list and the table view supports cut, copy, and paste.
  • The text fields in the Details Panel, the Filter text field, and the Notes text pane support cut/copy, paste, and drag and drop.
  • The Company icon region in the Details panel accepts a dropped image (jpg, png, gif, or tif).

Read the tutorial

  Desktop Java Platform Development

Adding Functionality to Buttons: A Beginners Guide Contributed

Contributed by Saleem Gul and Tomas Pavek, maintained by Ruth Kusterer

This tutorial teaches you how to build a simple GUI with back-end functionality. This tutorial is geared to the beginner and introduces the basic construction of a GUI with functionality. A basic understanding of the Java Programming Language is required.

This is a basic tutorial that takes the approach of teaching programming from the GUI development perspective.

  • The Matisse package is used to facilitate the GUI development.
  • Matisse is available in NetBeans IDE 5.0 or better.
  • We will learn how to develop a GUI and then add functionality to the buttons used.

This document takes you through the fundamental concepts of GUI creation and takes the approach taken in many self learning books. We will work through the layout and design of a GUI and add a few Buttons and Text Boxes. The Text Boxes will be used for receiving user input and also for displaying the program output. The Button will initiate the functionality built into the front end. The application we create will be a simple but functional calculator.

Expected duration: 15 minutes

Read this tutorial
  Server-Side Java Development

Downloading and Importing Ajax and Other Components Contributed by the Visual Web Pack Tutorials Team

This tutorial shows how to download a component module from the NetBeans Visual Web Pack 5.5 Update Center and import the module into the IDE. The instructions are specific to downloading the sample BluePrints AJAX Component module for the first time. If you are downloading another component module, simply substitute the module name.

Contents

  • Downloading a Component Module
  • Importing the Module Into the IDE
  • Updating, Reverting, or Removing a Component Library

Content for this tutorial applies to NetBeans 5.5 and 5.5.1 Visual Web Pack

Note: If you installed the BluePrints AJAX Component library with the Technology Preview release, you must remove this library and then re-install the library for use with the production release. For instructions on removing a library, see Updating, Reverting, or Removing a Component Library.

Read the tutorial

  Java Technology Training

Java Technology Training: Instructor-Led, Self-Paced Web, CD, and Virtual Courses

  • Java Platform Overview for Managers (WJTB-310) :
    The J2EE Platform Overview for Managers bundle provides students with the key concepts and technical insights necessary to manage or proscribe distributed application development with the Java SE Platform, and the Enterprise Edition.
  • Introduction to Developing Rich-Client Applications (WJO-1107) :
    This course defines the concept of a Java technology rich-client application, also known as a Swing application, and describes how to use the Swing API. Students learn how to use the features of the NetBeans IDE for rapid application development. The course demonstrates how to extend the NetBeans platform to build a simple Swing application.

  • Developing Applications for the J2EE Platform (CDJ-310A) :
    This course provides students with the knowledge to build and deploy enterprise applications that comply with Java 2 Platform, Enterprise Edition (J2EE) standards. Students are taught how to assemble an application from reusable components and how to deploy an application in the J2EE platform runtime environment.

See the course catalog

  For More Information


To comment about the content of this newsletter, send an email to fundamentals_newsletter@sun.com.

© 2007 Sun Microsystems, Inc. All rights reserved. For information on Sun's trademarks see: http://www.sun.com/suntrademarks

To unsubscribe from this list, reply to this message with "Unsubscribe" in the subject line or use this link:
http://communications1.sun.com/r/c/r?2.1.3J1.2Vd.12SMlS.C4ZfU4..H.Eewi.1m0y.aT1raXJpLnRlY2hAZ21haWwuY29tHZOHHB00

To manage your Sun subscriptions, visit the Subscription Center.

Sun Microsystems, Inc., 10 Network Circle, MPK10-209 Menlo Park, CA 94025 U.S.A.





 
Comments: Post a Comment



<< Home
News, Articles, events from all over the world

My Photo
Name:
Location: India

Born on shraavana shudha chauthi of dundubhi naama samvaswara, Im kiran alias kini alias kiri bought up by loving parents. Being from agricultural family I have learnt plowing, carting but never learnt climbing trees. Now away from home I have lost touch with the agricultural skills.

ARCHIVES
January 2006 / February 2006 / March 2006 / April 2006 / May 2006 / June 2006 / July 2006 / August 2006 / September 2006 / October 2006 / November 2006 / December 2006 / April 2007 / May 2007 / June 2007 / July 2007 / August 2007 / September 2007 / October 2007 / November 2007 / December 2007 / January 2008 / February 2008 / March 2008 / April 2008 / May 2008 / June 2008 / July 2008 / August 2008 / September 2008 / October 2008 / November 2008 / December 2008 / January 2009 / February 2009 / March 2009 / April 2009 /


Powered by Blogger