|
Spec-Zone .ru
спецификации, руководства, описания, API
|
|
Swing Enhancements in the Java TM Standard Edition 6.0 |
Documentation Contents |
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 was added to the JDK. The
SwingWorkerclass enables a long-running task to be executed on a background thread. A subclass ofSwingWorkermust implement the 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. TheSwingWorkerclass supports intermediate results. The background task can invoke the method, which sends partial data to the method for processing on the event dispatch thread. In addition, theSwingWorkerclass 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 section of the Swing tutorial.
Text Printing
Several new methods of the 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 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 class. The class is an implementation of the
RowSorterclass that deals with grid-based models. The class was added to the package as an implementation of theRowSorterclass for JTables. TheTableRowSorterclass enables you to specify differentComparators and also provides a filtering option.For details, see the 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 and methods were added to the 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 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 class:
For details, see the 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 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 theRepaintManagerclass:
All Swing top-level components override the
repaintmethod, which calls the appropriateaddDirtyRegionmethod.The class now overrides the
setDoubleBufferedmethod, which defines whether this component should use a buffer for painting or not.GroupLayout Layout Manager
The layout manager was added to the
javax.swingpackage. This layout was developed for the GUI builder provided with the NetBeans IDE. TheGroupLayoutis primarily intended for use by GUI builders but may be hand-coded as well.For details, see the 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 .) The new mechanism introduces the property for components that support drops such as
JTree,JTable,JList, andJTextComponent. TheDropModeproperty 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,JTableINSERT-JList,JTree,JTable,JTextComponentON_OR_INSERT-JList,JTree,JTableON_OR_INSERT_ROWS, ON_OR_INSERT_COLS-JTableUSE_SELECTION-JList,JTree,JTable,JTextComponentFor 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
TransferHandlerclass with regard to handling drop operations. Previously, theTransferHandlerclass was unable to accomplish common drop-related tasks since import handling methods lacked necessary information. The new inner class provides details on every transfer operation.New overloaded versions of the and methods were added to the
TransferHandlerclass. These methods take an instance of theTransferSupportclass 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 theTransferSupportinstance. For example, the newcanImportmethod can be implemented to detect an attempt to move data from a child to its parent and reject such a transfer. Unlike previous versions, thecanImportmethod 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 thecanImportmethod 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
setTransferHandlerandgetTransferHandlermethods 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 .)
For details, see the section of the Swing tutorial.
Miscellaneous Changes
Ability to Reset a ButtonGroup to an Unselected State
The
ButtonGroupclass 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 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 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 asJRadioButtonandJCheckBoxinstances.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.hideActionTextproperty determines whether a button displays the text from theAction. To set and get this property, the following new methods were added to theAbstractButtonclass: and 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
nullto 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 model added to the JDK 6, AWT allows creation of truly parentless dialogs. This means that
d.getParent()returnsnullfor these dialogs. To make use of new modality API, JDialogs also need to be truly parentless (refer to the Bug Database, bug ID .)New constructors with a
Windowparameter as an owner were added to theJDialogclass and a new method was added to theJOptionPaneclass. To create a parentless JDialog, you can now passnullto either of constructorsJDialog(Dialog)orJDialog(Window). To create a parentless JOptionPane, use the 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()returnsnullfor them. For the third dialogd.getParent()returns the shared-owner frame.
Copyright © 1999-2008 All Rights Reserved.
![]()