12 Swing — Java Programming
12.1 Swing
The user interface components in application and applets can be built using AWT components provided by Java. Java also provides another alternative, supercharged components called Swing components. Swing is a set of classes that provides more powerful and flexible components than are possible with the AWT. In addition to buttons, check boxes, and labels, Swing provides several other exiting components such as buttons with additional capabilities, textarea with existing features, trees, tables, progress meters, tool tips and component organizers.
Unlike AWT components, Swing components are based on JFC (Java Foundation Classes) and are not implemented by platform specific code. Instead, they are written entirely in Java and therefore are platform-independent. So they are called lightweight components. The AWT components, which are platform dependent, are called heavyweight component.
The following table lists various swing components and their AWT counterparts:
|
AWT Components |
Swing Components |
|
Applet |
JApplet |
|
Button |
JButton |
|
CheckBox |
JCheckBox |
|
ComboBox |
J ComboBox |
|
Component |
J Component |
|
Dialog |
JDialog |
|
Frame |
JFrame |
|
Label |
JLabel |
|
List |
JList |
|
Menu |
JMenu |
|
Panel |
JPanel |
|
ScrollBar |
JScrollBar |
|
TextArea |
JTextArea |
|
TextField |
JTextField |
|
Window |
JWindow |
The main differences between AWT and Swing are
| AWT | SWING |
| Components are Platform dependent | Components are Platform independent |
| not Pluggable | Pluggable |
| limited components | powerful and large number of components |
| No MVC Architecture | Supports MVC |
*MVC means Model View Architecture.
12.2 Hierarchy of Java SWING classes
12.3 Components
| Method | Description |
|---|---|
| public void add(Component c) | add a component on another component. |
| public void setSize(int width,int height) | sets size of the component. |
| public void setLayout(LayoutManager m) | sets the layout manager for the component. |
| public void setVisible(boolean b) | sets the visibility of the component. It is by default false. |
12.4 TextField
The swing text field is encapsulated by the JTextComponent class, which extends Jcomponent. It provides functionality that is common to swing text components. An instance of the text field can be obtained by using one of the following constructors.
JTextField() JtextField(int cols) JtextField(String str, int cols) JtextField(String str)
Str is the string that should be present in the TextField by default and the cols is the number of columns in the text field.
12.5 Button
Swing buttons provide features that are not found in the Button class defined by AWT package. For example, an icon can be displayed on a swing button, different icons can be displayed on the button when it is disabled, pressed, or selected. A button can be created using one of the following constructors:
JButton(Icon ico) JButton(String str) JButton(String str, Icon ico)
Str is the te