Swing
Resources:
Quick reference
Layouts
To avoid deadlook starting a Swing program the main( ) should not call the Swing methods, but instead should submit a task to the event queue.
We can submit manipulations through
SwingUtilities.invokeLater( )
.
Code:
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class SwingConsole{
private SwingConsole() {
// Util Library
}
public static void run(final JFrame f, final int width, final int height) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
f.setTitle(f.getClass().getSimpleName());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(width, height);
f.setVisible(true);
}
});
}
}
JApplet, JFrame, JWindow, JDialog, JPanel, etc., can all contain and
display Components.
Class inheritance hierarchy diagrams

Layout manager
A Container uses a LayoutManager (aggregation relationship)
Container
In Container, there’s a method called
setLayout( )
that allows you to choose a different layout managerAll other JFC/Swing top-level containers contains aJRootPane as its only child. The content pane provided by the root pane should, as a rule, contain all the non-menu components displayed by the JFrame
JComponents
Container is used to group components. Frames, panels, and applets are examples of containers.
JComponent
is a type of Container
and a superclass of all the lightweight Swing components.
The GUI JComponent classes, such as JButton , JTextField , JTextArea , JComboBox , JList , JRadioButton , and JMenu , are subclasses of JComponent .
JButton
JButton jbtOK = new
JButton("OK");
System.out.println(jbtOK instanceof JButton);
System.out.println(jbtOK instanceof AbstractButton);
System.out.println(jbtOK instanceof JComponent);
System.out.println(jbtOK instanceof Container);
System.out.println(jbtOK instanceof Component);
- CheckBox and RadioBox inherit form JTogleButton. Both component behavior in the same way.
- AbstractButton can be added to a ButtonGroup.
JTabbedPane
http://download.oracle.com/javase/tutorial/uiswing/components/tabbedpane.htmlInserting a tab:
addTabmethod handles the bulk of the work in setting up a tab in a tabbed pane. The addTab method has several forms, but they all use both a string title and the component to be displayed by the tab.insertTabmethod, which lets you specify the index of the tab you're adding
setMnemonicAt(1, KeyEvent.VK_2);
pane.indexOfTabComponent(3)pane.remove(3)
| Component Events and Listeners | |||
|---|---|---|---|
| JComponent | User Interaction that Generates an Event | Event Object Created |
Listener Interface the Event Handler
Should Implement method(s) needed |
| JTextField | Pressing <Enter> | ActionEvent | ActionListener actionPerformed |
| JTextArea | |||
| JButton | Pressing the button | ||
| JRadioButton | Selecting a radio button | ItemEvent | ItemListener itemStateChanged |
| JCheckBox | (De)selecting a checkbox | ||
| JComboBox | Selecting an item | ||
| JList | ListSelectionEvent | ListSelectionListener valueChanged |
|
| Any component | Pressing/releasing mouse buttons | MouseEvent | MouseListener mousePressed mouseReleased mouseClicked mouseEntered mouseExited |
| Moving/dragging mouse | MouseMotionListener mouseDragged mouseMoved |
||
Helper Classes
The helper classes, such as Graphics
, Color
, Font
, FontMetrics
, Dimension
, and LayoutManager
, are not subclasses of Component
. They are used to describe the properties of GUI components, such as
graphics context, colors, fonts, and dimension.
NOTE
- JComponent not added during construction must call the
container's
validate()method in order to force a re-layout of the components (and thus the display of the new JComponent) - call
dispone()method to close a window
Swing Event Model
Note
- A listener receives a fired event and acts on that event
- Listeners, source (of an event) and handlers can be separated
- As a programmer, we create a listener object and register it
(
addXXXListener())with the componet that' firing the event addXXListenerVSremoveXXXListener()
