15

I have noticed that people recommend not intermixing Swing and AWT Components, however we see this alot:

import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.ImageIcon;
 //AWT imports though only for listeners
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

So why do many including Java (because I got that off their tutorial here) still use AWT imports, though I see its mainly for Listeners.

How do you add native Swing Listeners/Libraries for stuff like Key, Button, JComboBox presses/slections etc?

Or would I use firePropertyChangeListeners()? (though that relates to Java Beans)

It has been confusing me now for some time, most of my app have Swing and AWT which is said to be bad?

David Kroukamp
  • 36,155
  • 13
  • 81
  • 138
  • 7
    it's not about mixing the packages - it's about mixing the _components_, that is not add a Swing JSomething to a awt Canvas (or Frame or whatever) nor vice versa. Though that advice is a bit dated: since sometime in a late 6 update release the issues in mixing have been resolved (so now you can use a Canvas with 3D in a Swing component) – kleopatra Aug 20 '12 at 15:09
  • @kleopatra thank you for that now I undertsnad a bit more what was meant – David Kroukamp Aug 20 '12 at 15:14
  • 1
    See also [Java GUI listeners without AWT](http://stackoverflow.com/q/6255106/418556). – Andrew Thompson Aug 20 '12 at 23:22
  • @kleopatra 4 times out of 5 when people mix Swing & AWT components it is not an esoteric situation that justifies it. E.G. "My app. draws an image in the `Canvas` in a `JFrame`". – Andrew Thompson Aug 20 '12 at 23:25
  • @AndrewThompson yeah, but that wasn't the point here :-) – kleopatra Aug 21 '12 at 13:30

3 Answers3

11

Swing is built on top of AWT, with a different philosophy for creating and drawing UI components. Mixing UI components from the two frameworks could lead to unexpected results and was/is thus discouraged (as kleopatra states, this has been fixed). However, Swing still uses the AWT event queue paradigm, including listeners - it does not replace them with listeners native to Swing because there's no reason to.

Using both Swing and AWT for your applications is common practice, what you were warned against is using both Swing and AWT UI components.

Jacob Raihle
  • 3,720
  • 18
  • 34
10

Swing shares quite a few classes with AWT, and uses some of the same implementation - note that javax.swing.JComponent (the base Swing component class) actually inherits from java.awt.Component (the base AWT container class)

It's actually not that much of a problem to mix Swing and AWT if you are careful. The main pitfalls are:

  • You risk getting a very different look and feel if you mix AWT and Swing UI components
  • Swing components are "lightweight" (rendered by Java) while AWT components are "heavyweight" (implemented as components in the host platform) - this means you will have problems if you put AWT components inside Swing components (the other way round is fine)
mikera
  • 105,238
  • 25
  • 256
  • 415
  • 5
    finally found the article which explains the status of mixing component (since jdk6u12), havent read it, though ;-) http://java.sun.com/developer/technicalArticles/GUI/mixing_components/index.html – kleopatra Aug 20 '12 at 15:24
  • 1
    @kleopatra The article link appears broken. This seems like the right one; confirm pls? http://www.oracle.com/technetwork/articles/java/mixing-components-433992.html For future Googlers, it's titled Mixing Heavyweight and Lightweight Components By Sharon Zakhour and Anthony Petrov and begins "There are two kinds of graphics components in the Java programming language..." – Mark Peschel Jul 03 '17 at 22:17
  • I disagree that Swing components are lightweight; if anything, they are heavyweight. AWT uses native OS's code, while Swing makes Java render it. – Zimano Apr 24 '18 at 18:05
  • @Zimano lightweight, in this context, means that it does not consume GUI resources from the host OS - see e.g. https://stackoverflow.com/questions/416947/swing-components-are-light-weight – mikera May 02 '18 at 07:59
1

Maybe someone will see this in the future and still find it useful. There's a list of AWT components and what their Swing replacements are.

ChrisK
  • 158
  • 1
  • 13