Solved

Mixing Swing with AWT... why not?

Posted on 2002-03-06
7
548 Views
Last Modified: 2013-11-23
I have read in this forum that it is bad to mix Swing with AWT, but I don't understand why.  I have read that Swing uses 'heavyweight' components while AWT uses 'lightweight' components, but I don't understand what that means.

What is a 'heavyweight' component and what is a 'lightweiget' component?

Since all the Swing classes extend AWT classes, if AWT classes are 'heavyweight', how can the subclasses be 'lightweight'?  When you cast a Swing class to its AWT superclass, you lose functionality, so how is the Swing class 'lighter'?

And since the Swing classes extend AWT classes, you can say that a Swing class IS an AWT class.

Are there any other reasons to not mix them?  I've done it and it seems to work just fine.

What do I buy by limiting my component selection to one group or the other?
0
Comment
Question by:m_onkey_boy
7 Comments
 
LVL 9

Expert Comment

by:Venci75
Comment Utility
.
0
 
LVL 92

Accepted Solution

by:
objects earned 100 total points
Comment Utility
Heavyweights have native screen resources associated with them, while lightweights don't.
The component hierarchy is not the full implementation, the actual paint for example is handled by other classes.
You can mix the two but you need to take care when doing so.

Here's an article which hopefully explains it better than I can:

http://java.sun.com/products/jfc/tsc/articles/mixing/index.html
0
 
LVL 7

Expert Comment

by:tomboshell
Comment Utility
The way I understand it (I don't know if I am correct but I 'think' that it sounds good....) is that the awt methods were not truely platform independant.  They use the platform methods/objects to create the gui objects.  This resulted in different appearance on different systems.  My guess is that the heavy term is from using the system resources to build the objects.  The OSs have a lot of backward compatable code and other junk.  (a little weak there, but somewhere along those lines...ya thats it!)

The swing components extend the awt classes.  Extending a class allows for some code reuse and also brings in new capabilties.  It is the second part that makes the difference here.  For I believe that the swing components have greater access to the VM defined methods for creating a gui object and do not rely so HEAVILY on the current system that they are running on.  So, they are lighter.  

I also read (a while back and I don't remember where) that the awt classes were not 'truely' system/platform independant.

Tom
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 1

Expert Comment

by:andyhedges
Comment Utility
Right both are portable to all systems that fully implement the JVM spec.

The AWT classes merely invoke native gui components

The swing classes draw themselfs onto a low level native panel.

Thus a button in AWT is a native system button whereas in Swing it is a native low level panel with a button drawn on it by the class.

It is advisable not to mix Swing and AWT components because AWT component are heavyweight as described above. That means that AWT components will appear 'on top of' swing components when they compete for space.

Although it is true that Swing components does inhereit from AWT components ALL swing components override the drawing of themselves. Thus the contructs of their AWT superclasses are just used as pure data containers. It also had/has the benefit of ensuring that migration is relatively painless.  
0
 
LVL 4

Author Comment

by:m_onkey_boy
Comment Utility
This answer didn't bring anything that was not already provided in objects' link.
0
 
LVL 4

Author Comment

by:m_onkey_boy
Comment Utility
Thank you.  

I took from the article that an all-swing app is better than an all-awt app.  Also, if you are building an AWT app, adding a Swing component is a lot less risky than adding an AWT component to a Swing app.

I haven't done any gui work other than for fun every now and then, so I appreciate the info.
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
Happy to help :-)

http://www.objects.com.au
Brainbench MVP for Java 1
http://www.brainbench.com
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now