Solved

Mixing Swing with AWT... why not?

Posted on 2002-03-06
7
557 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
custom annotations 9 38
How to execute a Python program and gather return output in Java 2 30
How to convert from xls to xlsx using java 7 39
mysql jsp example issue 32 35
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand columnThat will then direct you to their download page.From that page s…
The viewer will learn how to implement Singleton Design Pattern in Java.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

773 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