Documentation Contents |
Swing Enhancements in the Java TM Standard Edition 6.0 |
Major Features
Miscellaneous Changes
- Platform Look and Feel Improvements
- SwingWorker
- Text Printing
- JTable Sorting and Filtering
- Layout Enhancements
- Arbitrary Components as Tabs of JTabbedPane
- True Double Buffering
- GroupLayout Layout Manager
- Drag and Drop Enhancements
- Ability to Reset a ButtonGroup to Unselected State
- New Constants for the javax.swing.Action Interface
- Support for True Parentless JDialogs
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 ofSwingWorker
must implement thedoInBackground()
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. TheSwingWorker
class supports intermediate results. The background task can invoke thepublish
method, which sends partial data to theprocess
method for processing on the event dispatch thread. In addition, theSwingWorker
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 theRowSorter
class for JTables. TheTableRowSorter
class enables you to specify differentComparator
s 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
, andJDialog
) and supports new painting architecture based on a component's location in the window. The following new methods were added to theRepaintManager
class:
- addDirtyRegion(Applet, int, int, int, int)
- addDirtyRegion(JComponent, int, int, int, int)
- addDirtyRegion(WIndow, int, int, int, int)
All Swing top-level components override the
repaint
method, which calls the appropriateaddDirtyRegion
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. TheGroupLayout
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
, andJTextComponent
. TheDropMode
property is used to determine the mode by which a component tracks and indicates a drop location. To set this property for a component, thesetDropMode(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:
ON
-JList
,JTree
,JTable
INSERT
-JList
,JTree
,JTable
,JTextComponent
ON_OR_INSERT
-JList
,JTree
,JTable
ON_OR_INSERT_ROWS, ON_OR_INSERT_COLS
-JTable
USE_SELECTION
-JList
,JTree
,JTable
,JTextComponent
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, theTransferHandler
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 theTransferSupport
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 aTransferable
, a drop location, or a drop action associated with a transfer retrieved from theTransferSupport
instance. For example, the newcanImport
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, thecanImport
method is called continuously by a component'sTransferHandler
, 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 thecanImport
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
andgetTransferHandler
methods added to the class of each top-level component (JApplet
,JWindow
,JFrame
, andJDialog
).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 an Drop and Data Transfer section of the Swing tutorial.
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
SELECTED_KEY
This key is used to store the selected state of a component. It is used for components with meaningful selection state such asJRadioButton
andJCheckBox
instances.DISPLAYED_MNEMONIC_INDEX_KEY
This key is used to specify an index in the text at which a mnemonic decoration should be placed.LARGE_ICON_KEY
This key is used to store an icon.hideActionText
property determines whether a button displays the text from theAction
. To set and get this property, the following new methods were added to theAbstractButton
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 constructorJDialog(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()
returnsnull
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 theJDialog
class and a new method was added to theJOptionPane
class. To create a parentless JDialog, you can now passnull
to either of constructorsJDialog(Dialog)
orJDialog(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 operationd.getParent()
returnsnull
for them. For the third dialogd.getParent()
returns the shared-owner frame.
Copyright © 1993, 2010, Oracle and/or its affiliates. All rights reserved. Please send comments using this Feedback page. |
Java Technology |