Srikanth Technologies

New Features of Java 6.0

Java SE 6.0 is not yet released as of now. The most recent is beta 2 of Java SE 6.0. The final release is expected any time in the near future.

But many have the chance to use it for a long time because of Sun has taken a different direction in the development of Java SE 6.0. It makes a snapshot of Java 6.0 available at the end of every week to allow developer to see what is added in that week. So many have already understood every new features added by Sun in Java 6.0 even before the version is finally released.

In this article, I will explain some of the new features of Java 6.0. Language-wise Java 6.0 is no different from Java 5.0. The bulk of the change is related to Desktop applications.

New Methods in File Class

File class of java.io package is enhanced to provide methods related to get free and total disk space of the given drive.

Methods to make a file readonly, writeonly and executable are also provided. New methods added are listed below.

long   getTotalSpace()
long   getFreeSpace()
long   getUsableSpace()

boolean   setReadable( boolean)
boolean   setWritable( boolean)
boolean   setExecutable( boolean)
The following program displays total and free space for the drive that is passed from the command line.

import java.io.*;
public class  DiskSpace {
  public static void main(String args[]) {
   File f = new File(args[0]);

   System.out.printf("Total Space    : %d  MB\n", f.getTotalSpace() / (1024 * 1024) );
   System.out.printf("Available Space: %d  MB\n", f.getFreeSpace() / (1024 * 1024) );
  }
}
File class now also supports filename that have more than 255 characters.

Splash Screen At Startup

A new flag is provided for JAVA.EXE, which displays the given image as splash screen at startup of the application. This is used to reduce the response time at the startup giving the illusion to end-user that application started quickly even though application takes some time to load.

The following program causes some delay intentionally to show the importance of splash screen.

import javax.swing.*;
public class  ShowSplash  extends JFrame
{
     public static void main(String args[])  throws Exception
	 {
	      Thread.sleep(1000);
	      ShowSplash f = new ShowSplash();
	      f.setSize(200,200);
	      f.setTitle("Splash Demo");
	      f.setVisible(true);
	      f.setDefaultCloseOperation( EXIT_ON_CLOSE);
      }
}
At the time of invoking this application use -splash as follows. logo.gif must be present in the current directory.

java -splash:logo.gif  ShowSplash
At the time of startup, image logo.gif is displayed and then the actual application is displayed. This is very similar concept used by MS Word, NetBeans and many other applications.

Launching desktop applications from Java

Java 6.0 allows you to invoke common desktop application like mail agent, browser, editors etc. using methods of Desktop class.

You have to obtain an object of Desktop class using getDeskTop() static method of Desktop class.

The following are different methods of Desktop class used to invoke applications.

void browse (URI )
void edit (File)
void mail()
void open (File)
void print (File)

The following program launches different applications depending upon the flag sent from command line.

import java.awt.*;
import java.net.*;
import java.io.*;

public class DesktopDemo
{
   public static void main(String args[])  throws Exception
   {
     Desktop d = Desktop.getDesktop();  // get desktop object

     if ( args[0].equals("b"))  // invoke browser
          d.browse( new URI(args[1]));
     else
       if ( args[0].equals("e"))
          d.edit( new File(args[1]) );
       else
          if ( args[0].equals("o"))
          d.open( new File(args[1]) );
       else
          d.print( new File(args[1]));
   }
}

Run the above program by passing flag to indicate what type of application is to be invoked ( b - browser, e - editor, o - open otherwise print). The following example shows how to invoke browser and load readme.html into it.


 java DesktopDemo b readme.html

Hooking application to Taskbar

Java 6.0 allows an application to be hooked to Taskbar Status Tray on Windows. The following steps are to be taken to hook an application to Taskbar.
  1. Get SystemTray object using getSystemTray() method of SystemTray class.
  2. Create an object of TrayIcon and associate an image, popup menu and title with that. Image is displayed in the tray, popup menu is displayed when user clicks on image with right button. Title is displayed as a tooltip when user moves mouse pointer on image.
  3. Add TrayIcon object to SystemTray object.
The following program illustrates these steps.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class  SystemTrayDemo
{
 public static void main(String args[])  throws Exception
 {
   TrayIcon trayIcon = null;

   SystemTray tray = SystemTray.getSystemTray();
   // load an image
   Image image = Toolkit.getDefaultToolkit().getImage("HOMELOGO.JPG");
   ActionListener listener = new ActionListener() {
             public void actionPerformed(ActionEvent e)
             {
               JOptionPane.showMessageDialog(null,"Srikanth Technologies","Title",JOptionPane.INFORMATION_MESSAGE);
             }
         };


   // create a popup menu
   PopupMenu popup = new PopupMenu();
   // create menu item for the default action
   MenuItem defaultItem = new MenuItem("Srikanth Technologies");
   MenuItem closeItem = new MenuItem("Close");
   closeItem.addActionListener( new ActionListener() {
             public void actionPerformed(ActionEvent e)
             {

                System.exit(0);
             }
         });


   defaultItem.addActionListener(listener);
   popup.add(defaultItem);
   popup.add(closeItem);

   trayIcon = new TrayIcon(image, "Srikanth Technologies", popup);
   trayIcon.setImageAutoSize(true);
   trayIcon.addActionListener(listener);
   tray.add(trayIcon);
  }

}

When you run the above program, an image appears in Taskbar. If you right click on that image a popup menu is displayed. Selecting option Srikanth Technologies will display a Frame with message,selecting Close option will close the application and remove the image from tray.

If you double click on the image then default option (close) is selected and application is closed.

Conclusion

Java 6.0 is more about providing new features related to desktop application. Core language remains the same. Though there are some enhancements related to core Api, nothing really significant in my opinion.

If you are creating more desktop applications with Java then Java 6.0 is worth your wait.