AWT vs. Swing....why use Swing?
Asked Answered
V

2

5

I have been doing some reading on AWT vs Swing, but I am not really clear on how Swing works. I have read that Swing sits on top of AWT and that it is lightweight (as opposed to AWT). My question is "how can it be lightweight if it uses AWT and inherits the AWT container?" I am confused. Why use Swing over AWT then....why not just use AWT? Would AWT slow the Swing components down?

Viviparous answered 8/7, 2014 at 2:54 Comment(2)
Swing is lightweight because it paints its own components. Read up on Painting in AWT and Swing.Goatfish
See this answer for a number of reasons we would not use AWT based components. Though note that a lot of the rest of the AWT (D-n-D, Graphics and custom painting, images, printing, layouts, event listeners..) are typically used by Swing components virtually unchanged.Jacklin
H
5

"how can it be lightweight if it uses AWT and inherits the AWT container?"

Lightweight means that the Swing component does not have native peer of it's own, it shares a (common) native peer. This native peer comes from the AWT container it is added to (this is typically the window) and is shared amongst all the Swing components within that container hierarchy...

AWT provides the "heavy" lifting, connecting to the native OS and providing the core channel through which Swing components get rendered. It also provides much of the native integration, such as the SystemTray, Desktop and per pixel translucency APIs which can be used by Swing

Why use Swing over AWT then....why not just use AWT?

This is matter of opinion, but generally, AWT was replaced by Swing and provides a much more flexible graphical API from which to develop. Because it doesn't rely on the platforms native components, it means you are free to develop components that you need and can be made to run across multiple platforms.

Swing also borrows much of the AWT API, including the Event Queue

JTree and JTable would be my first argument for using Swing over AWT ;)

Would AWT slow the Swing components down?

Not really. AWT has been using DirectX and OpenGL pipelines for some time now and because it's a translation layer between the native API's and the Java API's, it's generally pretty good at what it does. Besides, without AWT, you don't have Swing...

Harwood answered 8/7, 2014 at 3:9 Comment(2)
MadProgrammer - I have seen the word "borrowed" used in many articles including your answer here. Is this to say that Swing is actually only using AWT to paint the screen? As opposed to AWT actually using the native OS API's to create....say a button or text box?Viviparous
No, AWT is doing the painting, or more to the point, it provides access to the Graphics layer for Swing to paint on. Swing, unlike JavaFX, borrows a lot from the AWT API so it could take advantage of what was already available at the time. Things like Image, Graphics for example, come from the AWT API, but Swing brings a lot, include drag'n'drop, the transferable API, UndoManager, key bindings and inbuilt double buffering for example...Because Swing builds on top of AWT, it uses much of the API to expand it's power ;)Harwood
A
6

To understand how Swing is lightweight than AWT, you need to understand the concept of 'peer'. A peer is a widget provided by the operating system, such as a button object or an entry field object.

An AWT component is usually a component class which holds a reference with a peer interface type. This reference points to a native peer implementation.

When it comes to Swing, everything becomes clear and straight forward. Except the top containers, Swing implementation depends on nothing of individual platform. It has all the controls and resources. What Swing needs is event inputs to drive the system, and graphics, fonts and colors which are inherited from the top AWT containers. Ordinary Swing components can be seen as a logical area on AWT containers.

Swing components are called "lightweight" because they do not require a native OS object to implement their functionality. Swing components such as JButton, JTextArea, etc. are lightweight because they do not have an OS peer. Few Swing components may require an OS peer and would yield the similar performance as AWT.

Acceptable answered 8/7, 2014 at 3:9 Comment(0)
H
5

"how can it be lightweight if it uses AWT and inherits the AWT container?"

Lightweight means that the Swing component does not have native peer of it's own, it shares a (common) native peer. This native peer comes from the AWT container it is added to (this is typically the window) and is shared amongst all the Swing components within that container hierarchy...

AWT provides the "heavy" lifting, connecting to the native OS and providing the core channel through which Swing components get rendered. It also provides much of the native integration, such as the SystemTray, Desktop and per pixel translucency APIs which can be used by Swing

Why use Swing over AWT then....why not just use AWT?

This is matter of opinion, but generally, AWT was replaced by Swing and provides a much more flexible graphical API from which to develop. Because it doesn't rely on the platforms native components, it means you are free to develop components that you need and can be made to run across multiple platforms.

Swing also borrows much of the AWT API, including the Event Queue

JTree and JTable would be my first argument for using Swing over AWT ;)

Would AWT slow the Swing components down?

Not really. AWT has been using DirectX and OpenGL pipelines for some time now and because it's a translation layer between the native API's and the Java API's, it's generally pretty good at what it does. Besides, without AWT, you don't have Swing...

Harwood answered 8/7, 2014 at 3:9 Comment(2)
MadProgrammer - I have seen the word "borrowed" used in many articles including your answer here. Is this to say that Swing is actually only using AWT to paint the screen? As opposed to AWT actually using the native OS API's to create....say a button or text box?Viviparous
No, AWT is doing the painting, or more to the point, it provides access to the Graphics layer for Swing to paint on. Swing, unlike JavaFX, borrows a lot from the AWT API so it could take advantage of what was already available at the time. Things like Image, Graphics for example, come from the AWT API, but Swing brings a lot, include drag'n'drop, the transferable API, UndoManager, key bindings and inbuilt double buffering for example...Because Swing builds on top of AWT, it uses much of the API to expand it's power ;)Harwood

© 2022 - 2024 — McMap. All rights reserved.