Solved

Mixing Swing with AWT... why not?

Posted on 2002-03-06
7
558 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
ID: 6846608
.
0
 
LVL 92

Accepted Solution

by:
objects earned 100 total points
ID: 6846617
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
ID: 6846670
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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 1

Expert Comment

by:andyhedges
ID: 6847571
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
ID: 6847951
This answer didn't bring anything that was not already provided in objects' link.
0
 
LVL 4

Author Comment

by:m_onkey_boy
ID: 6847963
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
ID: 6848807
Happy to help :-)

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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
map related example 6 55
windows explorer path to command prompt 5 54
xampp tool 12 56
servlet example issue 6 46
Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
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:

828 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