Original URL: http://www.theregister.co.uk/2006/09/22/jse_bling/

Riding the Mustang

JSE 6.0 – more than 'bling generation' Java

By John Hunt

Posted in Developer, 22nd September 2006 10:15 GMT

The Java Platform, Standard Edition 6 JDK (variously called JSE 6.0, Mustang and, by many, J2SE 6.0; and, by some at least, JDK 6) has been in Beta 2 release format for some months and is nearing its actual release date (work started on this release around July 2005 and it is currently expected to be delivered in autumn/fall 2006).

As such, it is worth taking a brief look at what this new version of the core Java elements provides. In essence, J2SE 6.0 is basically the JDK 1.6 and builds on what has gone before - it certainly isn't as radical a development as the last release – it provides many "nice to have" features without, this time, actually affecting the core language.

Since the JSE 6.0 version has been released, I have been monitoring various discussion groups and forums to gauge informed opinion on its features. In fact, one of the most cutting comments I have seen described this version of Java as being "Java for the bling generation" - implying that it was full of eye candy style features but with little of substance. So is this fair?

What is in JSE 6?

Well, certainly, compared to the last release of Java (Java Standard Edition 5.0) the changes are far less fundamental – the language isn't changed, for example.

However, if you look under the covers, a number of features have been added which strengthen the previous release, and by the far the largest number of changes (at least from a developer's perspective) are in the area of GUIs – an area which was hardly touched by the last release. I must confess a certain love for GUI work (and Swing in particular) at this point, and a number of features I have wanted in the core Java have now appeared, including the ability to set the frame icon, system tray icon support, and a splash screen that doesn't need the whole JVM loaded before it will display. These may seem like little things, but for commercial-quality desktop software they are very important to the overall look and feel.

So what does the next release of Java have in store for us all?

Essentially it can be divided up into a number of areas:

Although there are many detailed features, including increased support for annotations, these are (for me) the main features of this release of Java.

Simple Example

As my particular passion is for all things GUI based, I will have a look at some of the Swing related features that I particularly like.

The following simple Java program illustrates some of the things I mentioned earlier.

package com.regdev.swing.Test;

import java.awt.AWTException;
import java.awt.Desktop;
import java.awt.Image;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Tester extends JFrame {
    private Desktop desktop;
    private JTextField url = new JTextField("http://", 25);
    private TrayIcon trayIcon = null;

    public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                Tester t = new Tester();
                t.pack();
                t.setVisible(true);
            }
        });
    }
    
    public Tester() {
        this.setTitle("Tester");
        if (Desktop.isDesktopSupported()) {
            desktop = Desktop.getDesktop();
        }
        initComponents();
    }
    
    private void initComponents() {
        setupFrameIcon();
        setupBrowserPanel();
        setupSystemTray();
    }
    
    private void setupFrameIcon() {
        ImageIcon imgIcon = new ImageIcon("images/icon.png");
        Image img = imgIcon.getImage();
        this.setIconImage(img);
    }
    
    private void setupSystemTray() {
        if (SystemTray.isSupported()) {
            SystemTray tray = SystemTray.getSystemTray();
            Image image = Toolkit.getDefaultToolkit().getImage("images/tray.gif");
            trayIcon = new TrayIcon(image, "Tester2");
            trayIcon.setImageAutoSize(true);
            trayIcon.addActionListener(new TrayActionListener());
            try {
                tray.add(trayIcon);
            } catch (AWTException e) {
                System.err.println("TrayIcon could not be added.");
            }
        }
    }
    
    private void setupBrowserPanel() {
        JPanel p1 = new JPanel();
        p1.add(url);
        JButton b = new JButton("Browse");
        p1.add(b);
        b.addActionListener(new URLActionListener());
        this.add(p1);
    }
    
    class URLActionListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            String target = url.getText();
            try {
                URI uri = new URI(target);
                if (desktop != null)
                    desktop.browse(uri);
            }
            catch(IOException ioe) {
                ioe.printStackTrace();
            }
            catch(URISyntaxException use) {
                use.printStackTrace();
            }
        }
        
    }

    class TrayActionListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            System.out.println("In here");
            trayIcon.displayMessage("Tester!", 
                "Some action performed",
                TrayIcon.MessageType.INFO);
        }
    };
}

The first thing to note is that this simple application checks to see that desktop support is available. This is done via:

if (Desktop.isDesktopSupported()) {
            desktop = Desktop.getDesktop();
        }

If this is not done then the application may throw a UnsupportedOperationException.

Next we set up the icon to use for the Frame. In Windows this is the icon that sits in the left hand corner of the main window. This is done using the setIconImage(Image i) method. In my case, this places a simple icon in the frame as illustrated below:

Example of a simple JSE 6 application

Figure 1: A simple JSE 6 Swing Application

In this case, the title for the window's frame is also set as "Tester". This means that on the Taskbar also shows this icon and the title Tester. This application also allows a URL to be entered into the JTextField in the main panel and for the Browser button to be used to display the specified URL in the current default browser. In the JSE 6.0 version of Java this is particularly straight forward requiring only:

desktop.browse(uri);

Finally, the last thing in this example to look at is that the application adds itself to the system tray. This is done using the following lines:

SystemTray tray = SystemTray.getSystemTray();
…
trayIcon = new TrayIcon(image, "Tester2");
…
tray.add(trayIcon);

The result of this is that in the System Tray I now have a simple icon representing my application:

A System Tray icon

Figure 2: A System Tray icon

The associated ActionListener also allows an action to be performed when the user selects the Icon in the tray.

One final feature that I tried out with this example, which is not illutrated in the source code, is the use of a splash screen. This splash screen is displayed by the underlying native system such that it can be displayed while the JVM is being started up – thus giving the user much needed confidence that their application is indeed running. To do this I used the splash option now available as a JVM option, for example:

java -splash:images/splash.gif com.regdev.swing.Test.Tester

The splash screen can also be specified in the manifest file for a Jar so that it does not need to be specified on the command line.

Summary

Although to many the new features in JSE 6 are far less revolutionary then those found in the last release of Java, they are certainly evolutionary and, for those who might use them, extremely useful. If nothing else the apparent performance improvement is welcome and the new features all eminently sensible.

References

https://jdk6.dev.java.net/ http://java.sun.com/developer/technicalArticles/J2SE/Desktop/javase6/desktop_api/index.html http://java.sun.com/developer/technicalArticles/J2SE/Desktop/javase6/enhancements/index.html http://java.sun.com/developer/technicalArticles/J2SE/Desktop/javase6/splashscreen/index.html http://weblogs.java.net/blog/shan_man/archive/2005/06/improved_drag_g.html http://www.mozilla.org/rhino