Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 583
  • Last Modified:

Mixing Swing with AWT... why not?

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
m_onkey_boy
Asked:
m_onkey_boy
1 Solution
 
Venci75Commented:
.
0
 
objectsCommented:
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
 
tomboshellCommented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
andyhedgesCommented:
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
 
m_onkey_boyAuthor Commented:
This answer didn't bring anything that was not already provided in objects' link.
0
 
m_onkey_boyAuthor Commented:
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
 
objectsCommented:
Happy to help :-)

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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now