Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swing and AWT Mixing is bad, but still done, why?

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?

like image 267
David Kroukamp Avatar asked Aug 20 '12 15:08

David Kroukamp


People also ask

Can AWT and Swing be used together?

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 .

Why swings are better than AWT?

Swing components provide the higher-level inbuilt functions for the developer that facilitates the coder to write less code. Java AWT needs a higher amount of memory for the execution. Java Swing needs less memory space as compared to Java AWT. Java AWT is slower than swing in terms of performance.

Is AWT better than Swing?

AWT is a thin layer of code on top of the OS, whereas Swing is much larger. Swing also has very much richer functionality. Using AWT, you have to implement a lot of things yourself, while Swing has them built in. For GUI-intensive work, AWT feels very primitive to work with compared to Swing.


2 Answers

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.

like image 177
Jacob Raihle Avatar answered Oct 17 '22 15:10

Jacob Raihle


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)
like image 27
mikera Avatar answered Oct 17 '22 15:10

mikera