Client-Side Improvements
in Java 6 and Java 7
Learn about the
improvements to the client and desktop parts of Java SE 6 and Java SE 7,
including the new applet plug-in, the Java Deployment Toolkit, shaped and
translucent windows, heavyweight-lightweight mixing, and Java Web Start.
Introduction
Since the release of
Java Platform, Standard Edition 6 (Java SE 6) in December of 2006, a lot of improvements have been made to the client and desktop parts of Java. In this
article, we take a tour of Swing, and then we dive into some of the support
technologies that let developers make great client apps, such as installation,
Java Web Start, applets, and graphics improvements. Then we will take a peek at
Java 7.
Java SE 6 brought
several new APIs to Swing. The Drag and Drop API was extended with the
TransferSupport class, which made implementing drag-and-drop support in Swing
applications much easier and gave developers the opportunity to build live
feedback into their controls. Parts of the Swing Tutorial show how to use it.
While Java SE 6 did not introduce any new Swing controls, it did significantly improve the JTable control by adding an API for sorting and filtering table content. This API lets you sort a table by columns or filter the data inside a table using text searching, and it also lets you create custom sorters and filters for your own data types. The Swing Tutorial shows how to use the JTable Sorting and Filter API that was added in Java SE 6.
Responsiveness has always been an important feature of good client applications. A well-designed app should push any significant work into a background thread. However, synchronizing a thread with the Swing event and painting mechanism has always been tricky. Java SE 6 introduced the SwingWorker utility class to make working with background tasks much simpler. SwingWorker spawns a background thread and then lets you safely provide progress feedback to your UI on the Swing Event thread.
While Java SE 6 did not introduce any new Swing controls, it did significantly improve the JTable control by adding an API for sorting and filtering table content. This API lets you sort a table by columns or filter the data inside a table using text searching, and it also lets you create custom sorters and filters for your own data types. The Swing Tutorial shows how to use the JTable Sorting and Filter API that was added in Java SE 6.
Responsiveness has always been an important feature of good client applications. A well-designed app should push any significant work into a background thread. However, synchronizing a thread with the Swing event and painting mechanism has always been tricky. Java SE 6 introduced the SwingWorker utility class to make working with background tasks much simpler. SwingWorker spawns a background thread and then lets you safely provide progress feedback to your UI on the Swing Event thread.
Graphics Improvements
Although Oracle has not
released any new public APIs for Swing since the release of Java SE 6, lots of
work has been done under the hood to make Swing faster and more efficient.
Let's start with the graphics layer.
First, the client team at Oracle made drawing faster by enabling the Direct3D and OpenGL pipelines by default. This means that whenever you draw something on the screen, the operating system can use hardware acceleration available in your graphics card to speed up the drawing.
Previously, only functions such as drawing lines and rectangles could be accelerated. Today, however, most computers come with programmable GPUs that can accelerate more-complicated drawing functions. If you have the latest update of Java SE 6 on a recent Microsoft Windows computer, then BufferedImageOps can be accelerated by a factor of ten. This means you can apply effects such as blurs, shadows, and saturation adjustment in real time. With the new OpenGL and Direct3D pipelines, you can make blazing fast apps, all in 100% Java.
Along with hardware acceleration, the Swing team improved the way graphics are drawn to the screen by using a back buffer. This means your app is drawn into a chunk of main memory first, and then it is drawn to the real screen a split second later. This new strategy has two advantages. First, animation should be very smooth with no tearing because the OS itself can schedule screen drawing. Second, if your application slows down for some reason, it won't leave a huge gray area on the screen while the computer waits for your app to respond. This so-called “gray rect” problem is now a thing of the past.
Abstract Windowing
Toolkit’s Frame and Window Classes
You might think AWT is
dead and gone, but it's actually two parts, one of which is still relevant. The
UI widgets (buttons, sliders, and so on) are no longer recommended for use.
However, AWT also includes the top-level Frame and Window classes. Oracle has
added a valuable non-public API: shaped and transparent windows.
In 2005, I co-wrote a book, <i>Swing Hacks</i>, that showed how to hack Swing to do all sorts of things. One of the most popular hacks was a method for faking shaped and transparent windows. We no longer have to fake it. Developers can now create an app with any window shape using the com.sun.awt.AWTUtilities class.
This class lets you configure your windows with a clip shape, a transparency value, or even a full alpha mask for per-pixel transparency. The following code sets the transparency of a window to 50%:
In 2005, I co-wrote a book, <i>Swing Hacks</i>, that showed how to hack Swing to do all sorts of things. One of the most popular hacks was a method for faking shaped and transparent windows. We no longer have to fake it. Developers can now create an app with any window shape using the com.sun.awt.AWTUtilities class.
This class lets you configure your windows with a clip shape, a transparency value, or even a full alpha mask for per-pixel transparency. The following code sets the transparency of a window to 50%:
com.sun.awt.AWTUtilities.setWindowOpacity(window,
0.5);
With Java SE 7, the API is public. Along with support for shaped windows, AWT now lets you mix heavyweight and lightweight controls. Swing controls are lightweight, meaning they simply draw to the screen with plain graphics calls. Other AWT components are heavyweight, meaning they use special operating system calls to draw to the screen.
Mixing lightweight and heavyweight controls has always been buggy and error-prone, which caused problems for anyone wanting to embed third-party native controls such as Web browser or OpenGL 3D canvas into their app. Java SE 6 update 12 introduced the ability to seamlessly mix heavyweight and lightweight controls. For the most part, you don't need to do anything new; it will just work.
Look and Feel
Java SE 6 included a lot
of work for improving the Windows Look and Feel. Originally, this look and feel
simulated the standard Microsoft Windows controls. In Java 2 Platform Standard
Edition 5.0 (J2SE 5.0), the Windows Look and Feel began using the native
theming engine of Microsoft Windows XP to ensure that Swing controls looked
just like their native counterparts, even if the user had switched to a custom
theme. Since the release of Java SE 6, the Swing team has continued to improve
the Windows Look and Feel with support for Microsoft Windows Vista and
Microsoft Windows 7, including the nice, subtle animations on focusable
elements.
J2SE 5.0 introduced a new skinnable look and feel called Synth that was greatly improved in Java SE 6. Since Java SE 6, the Swing team put in further Synth improvements, and they also created a whole new look and feel called Nimbus. Built by talented Oracle engineer Jasper Potts, Nimbus is a modern cross-platform look and feel with rounded edges, gradient fills, and subtle shadows. With Nimbus, there is no reason to use Metal anymore.
J2SE 5.0 introduced a new skinnable look and feel called Synth that was greatly improved in Java SE 6. Since Java SE 6, the Swing team put in further Synth improvements, and they also created a whole new look and feel called Nimbus. Built by talented Oracle engineer Jasper Potts, Nimbus is a modern cross-platform look and feel with rounded edges, gradient fills, and subtle shadows. With Nimbus, there is no reason to use Metal anymore.
Installation and Startup
A few years ago the Java
team initiated an effort called Java SE 6 Update N to address many of the
issues involved in getting Java installed, updated, and started faster. This
effort has resulted in a better initial user experience with Java.
First, the Java deployment team created a tool called the Java Deployment Toolkit. This toolkit is a collection of detection scripts and in-browser installers that ensure that the user has Java installed in order to launch the app quickly. All you have to do as a developer is include a special JavaScript file in your Web page and then call a function on the deploy Java object.
The following example includes the deployJava.js file, which then launches an applet. The toolkit detects the version of Java already installed (if any), launches a new installer (if needed), and launches the app.
First, the Java deployment team created a tool called the Java Deployment Toolkit. This toolkit is a collection of detection scripts and in-browser installers that ensure that the user has Java installed in order to launch the app quickly. All you have to do as a developer is include a special JavaScript file in your Web page and then call a function on the deploy Java object.
The following example includes the deployJava.js file, which then launches an applet. The toolkit detects the version of Java already installed (if any), launches a new installer (if needed), and launches the app.
<script src="http://www.java.com/js/deployJava.js"></script>
<script>
var
attributes = {codebase:'http://java.sun.com/products/plugin/1.5.0/demos/jfc/Java2D',
code:'java2d.Java2DemoApplet.class',
archive:'Java2Demo.jar',
width:710,
height:540} ;
var parameters =
{fontSize:16} ;
var version = '1.6' ;
deployJava.runApplet(attributes, parameters, version);
</script>
In addition to the Java
Deployment Toolkit itself, the Java team has improved the Microsoft Windows
installer to make it start faster, download less, and require fewer clicks.
They have also introduced a technology called the Java Kernel, which will
download just the parts of the Java Runtime Environment (JRE) required to get
your application running, and then install the rest in the background.
Also available on Microsoft Windows is a new background process called Java Quick Starter, which consists of a small system service that preloads portions of Java (only up to 20 MB) on Microsoft Windows XP.
Also available on Microsoft Windows is a new background process called Java Quick Starter, which consists of a small system service that preloads portions of Java (only up to 20 MB) on Microsoft Windows XP.
Java Web Start
Once Java is installed
and running on a user’s computer, there are two ways to launch your
application. If you want your app to run outside the browser with a desktop
icon, you should use Java Web Start. Java Web Start has come a long way since
its introduction nearly a decade ago. Java SE 6 added support for splash
screens, PNG format icons, and better control over updates.
Java Web Start lets you use a special form of compression called Pack200. It compresses Java bytecode by removing redundant information that is shared between classes in the same JAR file. Combined with GZip, Pack200 can result in a compression ratio of five to ten times, which can make a huge difference in startup time.
Originally, developers had to use a special servlet or reconfigure their Web server to support Pack200 downloads, but this is no longer the case. Java Web Start now auto-detects the Pack200 files on your Web server and downloads any that are available, falling back to the regular JAR files if no Pack200 files are found.
In addition to some bug fixes and speed improvements, the deployment team has added a few new APIs to Java Web Start. Developers can now create a custom loading progress bar for their apps using the DownloadServiceListener API and designating a particular JAR file in the app as the download indicator. With this new feature, developers can create a loading animation that is actually relevant to their apps, rather than using a generic progress bar.
In addition to the DownloadServiceListener, there is also the new DownloadService2 and IntegrationService. DownloadService2 lets you query the Java Web Start cache to see what resources and JAR files are already installed in the app and to check for updates available on the Web server. The IntegrationService API lets you create and delete shortcuts on the desktop and system menu, as well as edit system-wide MIME type associations for your app.
Java Web Start lets you use a special form of compression called Pack200. It compresses Java bytecode by removing redundant information that is shared between classes in the same JAR file. Combined with GZip, Pack200 can result in a compression ratio of five to ten times, which can make a huge difference in startup time.
Originally, developers had to use a special servlet or reconfigure their Web server to support Pack200 downloads, but this is no longer the case. Java Web Start now auto-detects the Pack200 files on your Web server and downloads any that are available, falling back to the regular JAR files if no Pack200 files are found.
In addition to some bug fixes and speed improvements, the deployment team has added a few new APIs to Java Web Start. Developers can now create a custom loading progress bar for their apps using the DownloadServiceListener API and designating a particular JAR file in the app as the download indicator. With this new feature, developers can create a loading animation that is actually relevant to their apps, rather than using a generic progress bar.
In addition to the DownloadServiceListener, there is also the new DownloadService2 and IntegrationService. DownloadService2 lets you query the Java Web Start cache to see what resources and JAR files are already installed in the app and to check for updates available on the Web server. The IntegrationService API lets you create and delete shortcuts on the desktop and system menu, as well as edit system-wide MIME type associations for your app.
Applets
Java Web Start isn't the
only app type that received an overhaul. Applets have been massively improved
as well. The first major change was rewriting the applet browser plug-in from
scratch. Applets are now run out-of-process, which means they are separate from
the browser so the browser can't be locked or crashed by a rogue applet. This
also makes applets more secure.
Along with the basic security of the new plug-in, applets can now use the full Java Web Start APIs for secure access to local files and storage, printing, access to the clipboard, and access to the Java Web Start cache. The rewritten plug-in also has a new Java-to-JavaScript bridge that lets you call JavaScript functions in a Web page, and vice versa. Some developers have used this to add file upload and local storage support to their Web applications. Applets can also create custom loading screens instead of using the default Java spinner.
The expanded Java Web Start security model for applets includes support for crossdomain.xml files. Normally, an unsigned applet cannot break out of the sandbox to open an HTTP connection to other Websites. However, there are times you want to allow this, such as when you are building mashups of Web services from third parties such Flickr, Google, or Twitter. To support this scenario, Java can now process crossdomain.xml files. Owners of Web services put this type of small file on their Websites. If the applet tries to connect to an external Web service, the plug-in first looks for the crossdomain.xml file on the site to see if opening a special connection is allowed. Both Flash and Silverlight support crossdomain.xml files, and now Java applets do as well.
The new browser plug-in makes a lot of new things possible. One interesting side effect of the new plug-in running out-of-process is that you can actually drag a running applet out of the Web browser on to your desktop. So applets are no longer constrained to the confines of a Web page, as shown in Figure 1.
Along with the basic security of the new plug-in, applets can now use the full Java Web Start APIs for secure access to local files and storage, printing, access to the clipboard, and access to the Java Web Start cache. The rewritten plug-in also has a new Java-to-JavaScript bridge that lets you call JavaScript functions in a Web page, and vice versa. Some developers have used this to add file upload and local storage support to their Web applications. Applets can also create custom loading screens instead of using the default Java spinner.
The expanded Java Web Start security model for applets includes support for crossdomain.xml files. Normally, an unsigned applet cannot break out of the sandbox to open an HTTP connection to other Websites. However, there are times you want to allow this, such as when you are building mashups of Web services from third parties such Flickr, Google, or Twitter. To support this scenario, Java can now process crossdomain.xml files. Owners of Web services put this type of small file on their Websites. If the applet tries to connect to an external Web service, the plug-in first looks for the crossdomain.xml file on the site to see if opening a special connection is allowed. Both Flash and Silverlight support crossdomain.xml files, and now Java applets do as well.
The new browser plug-in makes a lot of new things possible. One interesting side effect of the new plug-in running out-of-process is that you can actually drag a running applet out of the Web browser on to your desktop. So applets are no longer constrained to the confines of a Web page, as shown in Figure 1.
Figure 1. Applets Are No Longer Constrained to Running inside Browsers |
Mac OS X Improvements
Not only has Java on the
desktop been improved in general, but Apple’s Java team has made many
improvements specifically for the Mac. The Mac Aqua Look and Feel provides variants of UI
controls that behave in the same way as regular controls but look slightly
different. For example, a set of buttons in a toolbar may have rounded corners
to group them together. Some controls have a special mini-version used for
floating palettes.
You can use the control variants in your Java application
simply by setting a few client properties on your Swing widgets. Here's an
example of converting a regular button into a textured button using a client
property:
jbutton.putClientProperty("JButton.buttonType",
"textured");
Figure 2. Rectangular Button before Conversion. |
Figure 3. Rectangular Button After Conversion |
In addition to control variants, the Apple team
has ported most of the Java SE 6 update features to recent versions of Java for
the Mac, including the new applet plug-in. Any Mac with Snow Leopard now has a
copy of Java that is feature-compatible with the latest Microsoft Windows JRE.
Even more exciting, Apple has announced that they are donating their code to the OpenJDK project. In the future, the Mac JRE won't be a special Apple build, but instead it will be another OpenJDK platform supported directly by Oracle. This means Mac users will get the same version of Java as everyone else, right from the source. An early alpha version of the Aqua Look and Feel for OpenJDK was recently announced. If you are interested in hacking on the new Mac JRE, please check it out.
Even more exciting, Apple has announced that they are donating their code to the OpenJDK project. In the future, the Mac JRE won't be a special Apple build, but instead it will be another OpenJDK platform supported directly by Oracle. This means Mac users will get the same version of Java as everyone else, right from the source. An early alpha version of the Aqua Look and Feel for OpenJDK was recently announced. If you are interested in hacking on the new Mac JRE, please check it out.
Java SE 7
The release of Java SE 7
brings more improvements. First, the great hardware acceleration enjoyed by Mac
OS and Microsoft Windows will be available on Linux in the form of XRender
improvements. Oracle will also be making final official versions of the shaped
and transparent window APIs introduced in the Java SE 6 updates. Swing gets its
first new component in a while, JLayer, which lets you seamlessly mix
transforms and special effects with regular Swing controls.
Finally, desktop Java apps benefit from many of the general improvements in Java SE 7. The improved file I/O APIs offer fine-grained control over file permissions, symbolic links, and notification of file changes, plus the ability to have the operating system move and copy files at native speed.
With the improvements in Java SE 7 and a new Java language version of JavaFX available this summer, things are looking bright for client-side Java.
Finally, desktop Java apps benefit from many of the general improvements in Java SE 7. The improved file I/O APIs offer fine-grained control over file permissions, symbolic links, and notification of file changes, plus the ability to have the operating system move and copy files at native speed.
With the improvements in Java SE 7 and a new Java language version of JavaFX available this summer, things are looking bright for client-side Java.
See Also
- Introducing Java 7
- Java SE desktop technologies
- TransferSupport class
- Java Tutorial, “Customizing the Loading Experience”
- Creating Shaped and Translucent Windows
- Creating Draggable Applets
- Java Deployment Toolkit
No comments :
Post a Comment