Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWT vs. Swing....why use Swing?

Tags:

java

swing

awt

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?

like image 259
cokeefe28 Avatar asked Jul 08 '14 02:07

cokeefe28


2 Answers

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.

like image 187
Juned Ahsan Avatar answered Oct 14 '22 22:10

Juned Ahsan


"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...

like image 39
MadProgrammer Avatar answered Oct 14 '22 21:10

MadProgrammer