4

I read on SO that mixing awt and swing is not really a good approach for GUI programming in Java. I cannot though, find any examples which does not use some awt components while using swing. For example, even when using swing, most of the example I have encountered will use awt for layout and event handling.

That being said, what does it mean not to mix swing and awt in GUI programming using Java? Does it just mean not to borrow graphical widgets such as button, canvas from swing and awt at the same time or just completely use swing or awt alone? Or it is okay to use graphical units ( like buttons, panels) from swing and event handling and layout from awt?

Most of the time the imports will look at least like this:

import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

I might be wrong but I have not seen example using completely swing without awt or looking at this in a wrong way since I am new to Java GUI.

Jack_of_All_Trades
  • 10,942
  • 18
  • 58
  • 88
  • 4
    Swing is built _on top of_ AWT, so you will use lots of classes from AWT when writing swing GUIs. The "don't mix Swing and AWT" refers to, as you correctly infer, not mixing GUI components like `Frame` and `JFrame`. You will notice that `JFrame` extends `Frame`. – Boris the Spider Sep 20 '13 at 13:54
  • It is really hard to avoid using AWT when using Swing since it's built on top of AWT. Sometimes you're even forced to use both of them together, I think [this](http://www.oracle.com/technetwork/articles/java/mixing-components-433992.html) is a good example of such a case. – Emiel Sep 20 '13 at 13:55
  • @BoristheSpider: Thanks for the comment. That helps clear my mind. Can you please elaborate your comment little and post as an answer for me to accept. – Jack_of_All_Trades Sep 20 '13 at 13:58
  • Usually, the only awt components you need are top level, and even those do not need to be used directly, as JFrame and JPopupMenu exist. Layout managers and events live in the awt namespace, but they're not components. – kiheru Sep 20 '13 at 13:59
  • @Jack_of_All_Trades: No problem, you will notice that heavyweight components (AWT) are used for stuff like embedding OpenGL surfaces etc. You could look at AWT as an implementation that is closer to the native GUI of the machine and Swing as an (almost pure) software implementation in Java (therefore it also supports theming). You might be interested to know that there also GUI alternatives which are completely software based ([Apache Pivot](http://pivot.apache.org/)). The Apache Pivot library is a very nice example of a gui that can be used anywhere (for example in an OpenGL environment). – Emiel Sep 20 '13 at 14:02
  • See this answer on [Swing extras over AWT](http://stackoverflow.com/a/6255978/418556) for many good reasons to abandon using AWT **components**. – Andrew Thompson Sep 20 '13 at 14:33

1 Answers1

6

You'll use the AWT layouts, action listeners and so on when using Swing - this is ok and by design, the Swing framework builds on AWT in this respect. This is in contrast to JavaFX which is a complete GUI framework in its own right, and contains its own layouts, event handlers and so on.

What you should avoid if possible is mixing the actual components you place in the GUI - using a JButton inside a Frame (rather than a JFrame) for example, or using a Button and JButton side by side.

There are some cases where it's necessary - using VLCJ in a Swing application for instance requires a heavyweight (AWT) canvas if you want to use a normal embedded video player. However, without a good use case like the above, it's something that should be avoided.

Michael Berry
  • 70,193
  • 21
  • 157
  • 216
  • 1
    With newer versions of Java mixing old AWT ("heavyweight") and new Swing ("lightweight") components should not be a problem: http://www.oracle.com/technetwork/articles/java/mixing-components-433992.html – lbalazscs Sep 20 '13 at 18:33
  • 1
    @lbalazscs Many of these bugs have been solved, but I've still seen painting issues with such mixing of components in the latest versions of the JRE. Granted, in many cases now it *won't* be a problem, but unless you've got a good reason to do so I see no reason to advocate the mixing of heavyweight and lightweight components - no point bringing up the potential for problems without good reason. – Michael Berry Sep 21 '13 at 08:12