[java] swing and awt

Started by
6 comments, last by davidsporn 15 years, 11 months ago
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.
Advertisement
Quote:Original post by agm_ultimatex
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.


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.
i realized that about setSize(). However eclipse gives me compile errors with setLayout and Graphics, unless of course i import java.awt.*
Quote:Original post by agm_ultimatex
i 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
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.
Quote:Original post by agm_ultimatex
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.

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

Quote:Original post by agm_ultimatex
i 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]
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][LWJGL Tutorials for NeHe][LWJGL Wiki][jMonkey Engine]
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

This topic is closed to new replies.

Advertisement