Documentation Contents

Swing Enhancements in the Java Standard Edition 6.0

Major Features
Miscellaneous Changes

Major Features

Platform Look and Feel Improvements

Both GTK and Windows look and feels were enhanced and improved by taking advantage of native rendering engines. All Swing components appear in accordance with the underlying platform's look and feel, provide appropriate feedback and have the correct size.

SwingWorker

An abstract class javax.swing.SwingWorker was added to the JDK. The SwingWorker class enables a long-running task to be executed on a background thread. A subclass of SwingWorker must implement the doInBackground() method, which performs the background computation. While the background task is being executed, the event dispatch thread processes events and provides any necessary updates to the UI. The SwingWorker class supports intermediate results. The background task can invoke the publish method, which sends partial data to the process method for processing on the event dispatch thread. In addition, the SwingWorker class supports property change listeners. Any change of a property triggers an event, which invokes event-handling methods on the event dispatch thread.

The use of the SwingWorker is recommended to accomplish time-consuming tasks. For details, see the Concurrency in Swing section of the Swing tutorial.

Text Printing

Several new methods of the javax.swing.JTextComponent class provide printing support for text documents. Supported formats include HTML, RTF, and plain text. The new API allows you to print the entire contents of a text component regardless of whether the component is visible or not. Features include printing interactively or non-interactively, displaying a print dialog, adding custom headers or footers, and automatic layout and pagination. The document content is formatted to fit the page size.

The new API includes the following methods:

For details, see the How to Print Text section of the Swing tutorial.

JTable Sorting and Filtering

To provide sorting and filtering capabilities, several new classes were added to the JDK. The basis for sorting and filtering is provided by the abstract RowSorter class. The DefaultRowSorter class is an implementation of the RowSorter class that deals with grid-based models. The TableRowSorter class was added to the javax.swing.table package as an implementation of the RowSorter class for JTables. The TableRowSorter class enables you to specify different Comparators and also provides a filtering option.

For details, see the How to Use Tables section of the Swing tutorial.

Layout Enhancements

The new API is intended for developers who write layout managers and includes the following two features: an ability to get the baseline for components and an ability to get the preferred spacing between components.

Professional layouts typically align components along their baseline. Previously, Swing provided no means to get the baseline of components. As a solution to this problem, the getBaseline(int,int) and getBaselineResizeBehavior() methods were added to the javax.swing.JComponent class to get the baseline and the baseline behavior when a component's size changes.

Each look and feel provides different guidelines regarding how far components should be placed from each other. In order to create a cross platform look and feel there should be an API for getting this information. As a solution, the javax.swing.LayoutStyle class was added. Layout managers can use this class to determine the preferred amount of space to place between components. Look and feel authors can provide a custom LayoutStyle by overriding the getLayoutStyle() method of the look and feel.

Arbitrary Components as Tabs of JTabbedPane

Prior to release 6, the content of a tabbed pane's tab was limited to a string or an icon (or both). The new API allows you to add custom components to tabs. The most common use of this feature is to add close buttons that remove tabs from tabbed panes. The following new methods were added to the javax.swing.JTabbedPane class:

For details, see the How to Use Tabbed Panes section of the Swing tutorial.

True Double Buffering

Previously, when a Swing based application was exposed after being hidden by another application, there was a noticeable delay between the moment when the background of the window was erased and the moment when the window painted. The problem has been resolved with adding true double buffering to Swing.

Each window has an off-screen image that is synchronised with the corresponding on-screen image. As a window is exposed, its off-screen image is directly copied on the toolkit thread to the screen. Another benefit applies to applications that block the event dispatch thread. If a user hides and then exposes an application window, the application still paints while the EDT is being blocked.

In addition, the RepaintManager class now handles painting for Swing top-level components (JApplet, JWindow, JFrame, and JDialog) and supports new painting architecture based on a component's location in the window. The following new methods were added to the RepaintManager class:

All Swing top-level components override the repaint method, which calls the appropriate addDirtyRegion method.

The JRootPane class now overrides the setDoubleBuffered method, which defines whether this component should use a buffer for painting or not.

GroupLayout Layout Manager

The GroupLayout layout manager was added to the javax.swing package. This layout was developed for the GUI builder provided with the NetBeans IDE. The GroupLayout is primarily intended for use by GUI builders but may be hand-coded as well.

For details, see the How to Use GroupLayout section of the Swing tutorial.

Drag and Drop Enhancements

Two major enhancements were introduced to the Drag and Drop feature in release 6.

First, the Drag and Drop operation can be configured not to use a selection for showing a drop location (refer to the Bug Database, bug ID 4468566.) The new mechanism introduces the DropMode property for components that support drops such as JTree, JTable, JList, and JTextComponent. The DropMode property is used to determine the mode by which a component tracks and indicates a drop location. To set this property for a component, the setDropMode(DropMode) method was added to the component's class for each component mentioned above. Listed below are available drop modes and the components that support them:

For backward compatibility the default value of this property is set to USE_SELECTION. To take advantage of the improved Drag and Drop feature, use one of the other drop modes mentioned above.

The second enhancement involves the TransferHandler class with regard to handling drop operations. Previously, the TransferHandler class was unable to accomplish common drop-related tasks since import handling methods lacked necessary information. The new TransferHandler.TransferSupport inner class provides details on every transfer operation.

New overloaded versions of the canImport and importData methods were added to the TransferHandler class. These methods take an instance of the TransferSupport class and thus allow developers to accept or reject transfers based on the details of the transfer. Developers can also customize drop behavior based on a Transferable, a drop location, or a drop action associated with a transfer retrieved from the TransferSupport instance. For example, the new canImport method can be implemented to detect an attempt to move data from a child to its parent and reject such a transfer. Unlike previous versions, the canImport method is called continuously by a component's TransferHandler, while a drag and drop operation takes place over the component. This feature provides the mechanism by which the acceptability of a transfer can be changed many times during the operation. For backward compatibility, the old version of the canImport method is called by the new version by default.

Another improvement includes addition of TransferHandler support to top-level components. This support is provided by means of the setTransferHandler and getTransferHandler methods added to the class of each top-level component (JApplet, JWindow, JFrame, and JDialog).

Previously users had to make a selection first and then click the selected item again to initiate a drag operation. Now a drag operation is initiated with a single mouse click (refer to the Bug Database, bug ID 4521075.)

For details, see the Drag and Drop and Data Transfer section of the Swing tutorial.

Nimbus Look and Feel

Nimbus, a cross platform look and feel introduced in the Java SE 6 update 10 release, is drawn with 2D vector graphics and can be rendered at any resolution. See Nimbus Look and Feel for more information.

 

Miscellaneous Changes

Ability to Reset a ButtonGroup to an Unselected State

The ButtonGroup class is used to create a multiple-exclusion scope for a set of buttons. Initially all buttons in a group are unselected. Previously there was no way to return to the unselected state after selecting a button. Now the clearSelection method allows developers to clear the selection so that none of the buttons in the group are selected.

New Constants for the javax.swing.Action Interface

The following constants were added to the Action interface.

The hideActionText property determines whether a button displays the text from the Action. To set and get this property, the following new methods were added to the AbstractButton class: setHideActionText(boolean) and getHideActionText() correspondingly.

Support for True Parentless JDialogs

In JDK 5.0 and earlier versions, the following line of code threw an exception:

JDialog d = new JDialog((Dialog)null);

Swing only allowed you to pass null to the constructor JDialog(Frame):

JDialog d = new JDialog((Frame)null);

In the latter case an invisible shared-owner frame was a parent for all parentless JDialogs.

With the new Modality model added to the JDK 6, AWT allows creation of truly parentless dialogs. This means that d.getParent() returns null for these dialogs. To make use of new modality API, JDialogs also need to be truly parentless (refer to the Bug Database, bug ID 6300062.)

New constructors with a Window parameter as an owner were added to the JDialog class and a new method was added to the JOptionPane class. To create a parentless JDialog, you can now pass null to either of constructors JDialog(Dialog) or JDialog(Window). To create a parentless JOptionPane, use the createDialog(String) method.

As a summary, have a look at the three constuctors below:

JDialog d = new JDialog((Dialog)null);

JDialog d = new JDialog((Window)null);

JDialog d = new JDialog((Frame)null);

Note the difference: although all three constructors take null, only the first two dialogs are truly parentless and the operation d.getParent() returns null for them. For the third dialog d.getParent() returns the shared-owner frame.


Oracle and/or its affiliates Copyright © 1993, 2016, Oracle and/or its affiliates. All rights reserved.
Contact Us