[java] swing and awt

Recommended Posts

I know there are problems mixing the two, so i want to try to use one. Id like to use swing as its lightweight, but I personally find awt has many essential parts to it. Such as setLayout(), setSize(), Graphics component, etc. Are there elements to swing that contains similar objects, or should I perhaps switch to using just awt.

Share on other sites
Quote:
 Original post by agm_ultimatexI know there are problems mixing the two, so i want to try to use one. Id like to use swing as its lightweight, but I personally find awt has many essential parts to it. Such as setLayout(), setSize(), Graphics component, etc. Are there elements to swing that contains similar objects, or should I perhaps switch to using just awt.

The Swing components all extend from AWT components. So setLayout(), setSize(), the Graphics object, and other AWT idioms are used in Swing as well. In general, Swing should always be preferred over AWT.

Share on other sites
i realized that about setSize(). However eclipse gives me compile errors with setLayout and Graphics, unless of course i import java.awt.*

Share on other sites
Quote:
 Original post by agm_ultimatexi realized that about setSize(). However eclipse gives me compile errors with setLayout and Graphics, unless of course i import java.awt.*

The problem with mixing AWT and Swing usually only pops up when mixing Components -- like mixing JMenus and Panels, the JMenu will not draw correctly over the Panel -- and not with the stuff that you're worrying about. You should be fine with using setLayout and Graphics in Swing.

If you plan on doing active rendering, however, you may want to use AWT instead of Swing, just because it is easier to get working correctly.

Greggles

Share on other sites
I can't say for sure as I lost all my school stuff. But I think when we did java gui, we used a regular Frame, JPanels, Jtextfields and all other J-Containers.

Share on other sites
Quote:
 Original post by agm_ultimatexI can't say for sure as I lost all my school stuff. But I think when we did java gui, we used a regular Frame, JPanels, Jtextfields and all other J-Containers.

Yeah, certain things do work together just fine (as far as I know what you are saying may work without any problems), but I guess the idea is that, when possible, you should try to keep your entire program consistant. The only problem is that sometimes you need to use certain AWT stuff with Swing [smile] So, don't worry about importing things like Graphics and Layouts, those should be fine.

Greggles

Share on other sites
Quote:
 Original post by agm_ultimatexi realized that about setSize(). However eclipse gives me compile errors with setLayout and Graphics, unless of course i import java.awt.*

When you use setLayout you might be doing something like this:
setLayout(new BorderLayout());

All the original layout managers are in the java.awt package. That is why you still have to import it.

You should also try to not use global imports like java.awt.*, you should instead specify each class that you want to import. This will help you understand where things are coming from so you can avoid misunderstandings like this in the future.

[Edited by - CaptainJester on May 25, 2008 7:06:22 AM]

Share on other sites
When I was playing with GL for Java, I had to use AWT heavyweight componant. But One in one of these panel, I put all that is needed to be able to use Swing component

Create an account

Register a new account

• Partner Spotlight

• Forum Statistics

• Total Topics
627662
• Total Posts
2978515

• 10
• 10
• 12
• 22
• 13