SWING

Swing is a package (library) in Java API that contains everything you need when you're doing Graphical User Interfaces.

Why not AWT?
AWT (Abstact Window Toolkit) is also a package with graphical components, but AWT is old and bad, and Swing was developed to replace it.
With AWT you will never be sure that your components in your interface look good, because their visual apperance change from a platform to another. There are serveral restictions, for example you can only use four different fonts, and you can never get a GUI with the same class as all the other GUIs in your operating system.
That was a reason for the Sun team to develop Swing, and from versions from Java 2 and forward there exsist a full package with Swing components to use.
All the swing components are lightweight, and completely programmed in Java. They are what you call Java Beans. If you like to run your GUI from the keyboard that works without writing any special code for that.

javax.swing
The main package is javax.swing, but most swing programs also need to import java.awt and java.awt.event, because some of the things to use is still there (some event handling, layout managers).

The most common components
In the swing package there are buttons, menus, scrollbars, panels, labels, textfields and many more graphical components. To separate them from AWT, which still exsists if someone must use them, the names in Swing always starts with a J. For example, JButton, JPanel, JLabel and so on.

In swing the containers have different status. They are dividied into a hierarchy with the so called top-level-containers in the top, and the atomic components in the bottom. In between there are intermediate components.

Top-level containers
A top-level container is a swing component with one only purpose, and that is to hold other swing components (with lower status). In Swing there are four top-level containers, but only three from those are used. They are JFrame, JApplet and JDialog. (JWindow is the fourth.) Each program using Swing must have atleast one top-level container, or else it won't work correctly. The most common is JFrame.

Intermediate container
An intermediate container is a component with the purpose of making the placement of the atomic components more easy. Sometimes you can't see them, for example is JPanel an intermediate container that is invisible. Other intermediate containers are JTabbedPane and JScrollPane. The most frequently used and most flexible is the JPanel.

Atomic component
The third class of component, the atomic components, are the most basic components in Swing. For example are JButton, JTextField and JComboBox atomic components. Some atomic components exsist for no other reason than presenting information for the user (JLabel), some are only used to get input from the user (JButton, JCheckBox), some can do both (JTable, JTextField). What its true for every atomic component is that they can't hold any other components. All atomic components descend from the JComponent class. This means that every atomic component supports the standard features, for example tool tips and borders.

JTable
JTable is an atomic component used to present information in form of a grid. A table can also be edited by the user, and this makes it a really flexible component. But it is also a little more complicated than a button. When you want to use a table you need to define a TableModel and a TableCellRenderer. The TableModel keeps track of all the data presented in the table, and the TableCellRenderer's task is to present the data graphically in the table.


For example is a table is a good way to present incoming mails.

When you install jdk on your computer you get a demo folder with it, and in there you can find a jar-file named SwingSet2.jar. It contains examples applications using of all (most?) swing components and also the source code for those examples. It's easier to understans how every component works that way.
And also looking in the javadoc Java API you can learn a lot.

Actions
When you have several components performing the same action, for example a menu item and a button (and that is in most a little more complicated GUI's the case), it is easy to use actions.

Themes / Look and Feel
With Swing you can change the whole visual apperance of an application using Look and Feel. If you want your application to have the common Windows Look and Feel that's easy, or if you want to have the Look and Feel of the current platform. There exsist many different themes you can use, you can even make your own if you want. And changing theme under runtime is not a problem.

Swing and threads
When you work with visible swing components in a GUI you have to watch out about the threads. The thread that handles all painting and repainting of swing components are called the event-dispatching thread. It is important that it is always this thread that is doing the painting. You can make sure it is this way by writing your program the right way, and in most cases you don't have to worry about the threads at all. But a rule to keep in mind is that no changes in the GUI's visual apperance (for example a label changing text) should be done outside the actionPerformed-methods. If you have to make a change somewhere else, you must take care of this.

Layout managers
To lay out the components within a container you use a layout manager. In Swing there are six different layout managers; BorderLayout, BoxLayout, CardLayout, FlowLayout, GridLayout and GridBagLayout. You can also create your own layout manager.

Listeners
Swing components can fire different kinds of events. This can for example be a mouse click, a mouse movement or a key click. If you want to know what events a special components can fire, just look in the javadoc Java API what listeners you can put on the component.




Swing resources

Java tutorial
Swing tutorial
Javadoc API
Thinking in Java (not only Swing)
Look and Feel guidelines