Swing Overview: The classes used to create GUI components of the following list are part of the Swing GUI components from package javax.swing. These are the latest GUI components of the Java2 platform. Swing components (as they are commonly called) are written, manipulated and displayed completely in Java language.
The following Swing Overview gives a few of the components from the javax.swing package.
|JLabel||An area where uneditable text or icons can be displayed.|
|JTextField||An area in which the user inputs data from the keyboard. The area can also display information.|
|JButton||An area that triggers an event when clicked.|
|JCheckBox||A GUI component that is either selected or not selected.|
|JRadioButton||To create a radio button|
|JComoboBox||A drop-down list of items from which the user can make a selection by clicking an item in the list or by typing into the box.|
|JList||An area where a list of items is displayed from which the user can make a selection by clicking once on any element in the list. Double-clicking an element in the list generates an action event. Multiple elements can be selected.|
|JPanel||A container in which components can be placed.|
|JColorChooser||We can create our own color palette.|
The original GUI components from the Abstract Windowing Toolkit package, java.awt (also called the AWT) are tied directly to the local platform’s graphical user interface capabilities. So, a Java program executing on different operating systems has a different appearance. But, the Swing components allow the programmer to specify a different look and feel for each platform, or a uniform look and feel across all platforms
Because javax.swing.JComponent class is the subclass of java.awt.Component class, we can use methods like setBackground(), setForeground() etc. to swing components like JButton and JTextField etc. Swing components are typically named as JXxx. Class JComponent is the superclass to most Swing components. This class defines the set of methods that can be applied to any subclass of JComponent.
Swing Overview: Swing components that subclass JComponent have many features including:
- A pluggable look and feel that can be used to customize the look and feel when the program executes on different platforms.
- Shortcut keys (called mnemonics) for direct access to GUI components through the keyboard.
- Common event handling capabilities for cases where several GUI components initiate the same actions in a program.
- Brief descriptions of a GUI component’s purpose (called tool tips) that are displayed when the mouse cursor is positioned over the component for a short time.
- Support for assistive(related) technologies such as Braille screen readers for blind people.
- Support for user interface localization — customizing the user interface for display in different languages and cultural conventions.
For other features refer: Java JFC Swing Introduction.
Note: In the following programs, importing java.awt package is necessary for Container class. Otherwise not necessary. If we take an anonymous object of getContentPane() and then add the JButton to it, the package is not needed to import at all as in the following:
JButton btn = new JButton(“OK”):
Similarly with, JApplet also, we need not import java.applet package because JApplet implicitly extends Applet class.
Properties of JFrame:
- As in the AWT, JFrame is the starting point for developing graphical applications.
- JFrame is one of the few Swing components that is built on an underlying "heavyweight" window.
- Components must be added to the content pane, not directly in the JFrame. Changing the properties like layout manager, background color, etc. should be applied to the content pane.
- Content pane can be accessed using getContentPane( ) method of JFrame.