Graphical resources and garbage collection --> Switching from AWT/Swing to SWT doesn't just mean learning a new API; it also requires former Swing programmers to change some of their habits and to care about new coding rules they didn't have to deal with in the Swing world. SWT uses a completely different philosophy than AWT
and Swing do when it comes to handling graphical resources. In AWT and
Swing, you can, in most cases, rely on the JVM's garbage collector to
free up graphical resources (image handles, colors, cursors, fonts,
widgets, etc.) when these are not needed anymore. I emphasize the words
"in most cases," because even in AWT this isn't always the case. For
example, a SWT uses a different approach: All SWT objects allocating platform resources ( To avoid resource leaks in an SWT application, you must follow this simple rule: If you instantiate an object that consumes graphical resources, you have to dispose of it yourself. Objects obtained from getters, diverse methods, or parameters should not be discarded by the code obtaining them, because the objects were not created there, and may be used by other parts of the application. The only exceptions are widgets: Disposing of a parent container will automatically dispose of all its children. If you follow this rule, you won't have any problem with memory leaks of graphical resources. Note that JFace provides helper classes and
frameworks to help you to manage and discard resources (fonts and
images) that may be shared by several components. These classes are
contained in the package If you want to get a better understanding of the rules listed above, and the reasons why SWT doesn't behave like AWT when managing graphical resources, read "SWT: The Standard Widget Toolkit, Part 2" by Steve Northover and Carolyn MacLeod. A link to this article is available in Resources. The most obvious difference between Swing and SWT is the component hierarchy. To facilitate the comparison between the Swing's and SWT component hierarchies, I've illustrated Swing's component tree in the following figure: The boxes with a yellow background represent ready-to-use widgets that can be deployed in a user interface. The boxes with a blue background represent abstract classes that are not intended to be used directly. As you can see, nearly all Swing components directly inherit from Now let's take a look at SWT's component hierarchy: As you can see, the number of available widgets here is pretty similar to what Swing offers, but the names and the hierarchy of the components is quite different.
The correspondence between each Swing component and its equivalent in SWT will be introduced in Widgets .
The equivalent of an AWT
However, there are some differences in this domain between AWT and SWT. If you look at the API documentation of SWT controls are automatically added to their parent
at construction time. When you construct an SWT control, the first
parameter required by the constructor is always the reference to the
parent composite. For this reason, Because the addition to the parent composite is done during the instantiation of a control, the order in which controls are instantiated defines the index the controls have in their parent. The index of a control in its parent may have an influence on the way the layout manager places it in the container. This can be an issue when porting existing Swing code, because in AWT/Swing, the order of instantiation of the children is not important -- in fact, a child can be instantiated before its parent. Only the order of addition of the children plays a role. When porting Swing code, you may have to change the order of creation of some widgets to get the same result as in Swing.
Like AWT, SWT makes use of layout managers to place children of a container. The layout algorithms that are available are different, however. To get an overview of the SWT layout algorithms, read "Understanding Layouts in SWT" by Carolyn MacLeod and Shantha Ramachandran. A link to this article is available in Resources. As in AWT, some SWT layouts require you to set some
layout constraints on widgets so that you can influence how the children
of a container are going to be laid out. In AWT, you set this
constraint by passing it as the second parameter to the method Data models and cell renderers vs. content providers and label providers One of the most beautiful aspects of Swing's
architecture is its strict adherence to the Model-View-Controller
pattern. The clean separation between model, view, and controller can be
above all observed in components like
SWT components don't have such a clean separation between model, view, and controller. If you create a table or tree using the SWT API only (that is, without using JFace), you'll probably miss the data models and cell renderers used in Swing. Creating a table using only the pure SWT API obliges you to create each row and each column like a standard control in a container, and to initialize them with rendered text and images. There is no support in SWT for data models. Fortunately, on top of the standard SWT controls,
JFace provides a framework that is comparable to the concepts used in
Swing. To use this framework, you have to instantiate a JFace viewer on top of the basic SWT table or tree. There are different viewers specialized for each kind of control: The equivalent of Swing's data model is in JFace called a content provider (see The equivalent of Swing's cell renderers in JFace is called a label provider (see For more information and examples of how to use JFace viewers, read the related articles in Resources. Like AWT and Swing, SWT lets your application react
to user interactions by registering event listeners on components. There
is not much difference in this area; the events thrown are all
subclasses of Of course, the hierarchy of the events and their associated listeners is different. In Widgets , we'll see what kind of events are thrown for SWT controls, and compare each to its Swing equivalent. |
|
來(lái)自: LibraryPKU > 《Java UI》