8
What are the main differences between the Swing and AWT interface construction libraries, regarding how to render the components of both and the performance?
8
What are the main differences between the Swing and AWT interface construction libraries, regarding how to render the components of both and the performance?
7
There are two types of components involved, the components Heavyweight and the lightweight. The components Heavyweight rely heavily on details implemented in native code to work (i.e., C and C++) and because of this, their performance tends to be (but not necessarily) a little better, but leaving them far more difficult to use, and sometimes depend on specific details of the operating system sacrificing compatibility. Already the components lightweight are made 100% in Java, without requiring native code.
The components Heavyweight are from the AWT (for example, java.awt.Button
), while the lightweight are those of Swing (for example, javax.swing.JButton
). Direct use of components Heavyweight is long discouraged from.
All components of AWT are subclasses of java.awt.Component
. One of those subclasses, java.awt.Container
is special because it corresponds to a component that may contain subcomponents. A special subclass of Container
is the class javax.swing.JComponent
which is the superclass of all Swing components. This hierarchy also means that any Swing components can contain subcomponents.
The design of all of them is based on the method paint(Graphics)
class Component
. However, it is not recommended to overwrite or invoke this method directly.
A redesign of a component can be forced with the use of the method repaint()
.
A component Heavyweight which has its dirty fabric design, can be redesigned by AWT through the method update()
. The standard implementation just clears the design area of the whole component and calls the paint()
to redesign it, but more specialized implementations can do incremental updates or determine which areas are dirty and redesign just those. Again, the method update()
should not be called directly.
However, the concept of incremental design and the use of update()
proved to be confusing and almost always unnecessary, so this is not used for the components lightweight. In the components lightweight, a call to update()
is in practice the same as a call to paint()
. In the components lightweight, the method paint()
implemented in the class JComponent
just calls the methods paintComponent(Graphics)
, paintBorder(Graphics)
and paintChildren(Graphics)
in this order (source). As the names suggest, they respectively draw the component, draw the edge of the component, and draw the subcomponents. Although you can write any of these methods, normally you will want to write only the paintComponent(Graphics)
.
In the end, in both cases, who performs the drawing is the class Graphics
. However, AWT will always use an instance of subclass Graphics2D
, then you can cast safely if you need to have access to a larger and better set of available methods.
And remembering that at the end of the day, who controls everything is AWT. Swing is just a layer over the AWT, not a complete replacement. Swing is only a replacement for AWT when it comes to replacing components Heavyweight by components lightweight.
There is one more important detail to note: JFrame
, JDialog
and JWindow
are not subclasses of JComponent
, then they don’t have the method paintComponent(Graphics)
, but have the method paint(Graphics)
. In this case, you may then prefer to write paint(Graphics)
invoking a super.paint(g)
within it. However, in the case of JFrame
s and JDialog
s, maybe it’s better to add a JPanel
inside and then write the paintComponent(Graphics)
of JPanel
.
TL;DR: That is, ultimately, what matters is that to implement the design of your subclass of JComponent
, you must write the method paintComponent(Graphics)
. For subclasses of JFrame
, JDialog
and JWindow
, you write about the paint(Graphics)
even.
More details on http://www.oracle.com/technetwork/java/painting-140037.html
3
In implemenation, the basic difference is that AWT uses the visual components Apis of the operating system where the VM is running to create entire components with virtually all the behavior being controlled by the OS.
Swing, on the other hand, draws components from scratch using basic graphical OS Apis, managing most of the components' operation on its own.
Browser other questions tagged java swing awt
You are not signed in. Login or sign up in order to post.
Based on your answer, then can you say that a swing interface is more portable than an AWT interface? Since it is independent of the OS?
– user28595
@diegofm Yes. The components of the AWT were something kind of rushed usingif hooks for the C and C++ components that Windows and Solaris offered at the time of Java 1.0 because the Sun people didn’t have time to implement this and couldn’t delay the release of Java because of it. However, these components are not flexible, difficult to use, and depend on the specific operating system. This was one of the ties that Swing came to release as an external library in Java 1.1 and embedded in the JRE of 1.2, being much more portable than the components of AWT.
– Victor Stafusa