Java Swing GUI
Swing is a GUI widget toolkit for Java that provides a
rich set of components for building graphical user
interfaces. Java Swing offers much-improved functionality over AWT,
new components, expanded components features, and excellent event
handling with drag-and-drop support.
AWT
Abstract Window Toolkit
set has quite a few bugs and does take up a lot of system resources when
compared to equivalent Swing resources.
Java AWT Java Swing
Swing is a part of Java Foundation
Java AWT is an API to develop
Classes and is used to create
GUI applications in Java.
various applications.
Components of AWT are heavy The components of Java Swing are
weighted. lightweight.
Components are platform Components are platform
dependent. independent.
Execution Time is more than
Execution Time is less than AWT.
Swing.
AWT components require Swing components requires
java.awt package. javax.swing package.
Core Class Hierarchy
1. Component: The root class of the Swing component hierarchy
○ Container: Extends Component; can hold other components
■ JComponent: Base class for most Swing UI components
■ JPanel, JScrollPane, etc.: Container components
■ JButton, JLabel, JTextField, etc.: Control components
2. Window: Top-level container (not visible on screen by default)
○ Frame: Represents a standard window with a title bar and borders
■ JFrame: Swing's version of Frame; the main application window
Key Structural Components
Containers
● JFrame: Main application window
● JPanel: General-purpose container for organizing components
● JDialog: Secondary window for dialogs
● JScrollPane: Container that provides scrolling for components
● JSplitPane: Container with two components separated by a divider
● JTabbedPane: Container with tabbed panes
Layout Managers
● Control the arrangement of components within containers:
○ BorderLayout: Arranges components in north, south, east, west, and center
regions
○ FlowLayout: Arranges components in a directional flow
○ GridLayout: Arranges components in a grid
○ BoxLayout: Arranges components either vertically or horizontally
○ CardLayout: Shows only one component at a time
○ GridBagLayout: Most flexible but complex layout manager
Control Class Purpose / How It Works
Label JLabel Displays static text or an icon. Not editable by the user.
Button JButton Clickable button that triggers an action (ActionListener).
Text Field JTextField Single-line input for text. Used to get user input.
Text Area JTextArea Multi-line text input area. Supports scrolling with JScrollPane.
Password Field JPasswordField Text field that hides input (e.g., for passwords).
Checkbox JCheckBox Allows toggling between checked/unchecked states.
Radio Button JRadioButton Select one option among many (grouped using ButtonGroup).
Combo Box JComboBox Drop-down menu with selectable options.
List JList Displays a list of items; can allow single or multiple selections.
Slider JSlider Allows the user to choose a value from a range using a sliding knob.
Spinner JSpinner Lets the user increment/decrement a value (numeric, date, or list-based).
Progress Bar JProgressBar Shows progress of a task as a bar filled from 0% to 100%.
Separator JSeparator Adds a horizontal or vertical line to separate sections.
Toggle Button JToggleButton Button that maintains its on/off state when clicked.
Toolbar JToolBar A container for holding a group of tool buttons.
Tabbed Pane JTabbedPane Allows switching between panels via tabs.
Table JTable Displays data in a 2D table format (rows and columns).
Tree JTree Displays hierarchical data in a tree format (like file systems).
Event Handling Architecture
Swing follows a delegation event model:
Components of the Event Model
● Event Sources: Components that generate events
● Event Objects: Packages of information about what happened
● Event Listeners: Objects that respond to events
Common Event Types
1. ActionEvent: Button clicks, menu selections, Enter key in text field
2. MouseEvent: Mouse clicks, moves, drags
3. KeyEvent: Keyboard input
4. FocusEvent: Component focus gained/lost
5. WindowEvent: Window operations (close, minimize)
6. ItemEvent: Selection changes in checkboxes, combo boxes
7. DocumentEvent: Text changes in text components
MVC Architecture in Swing
Swing follows the Model-View-Controller pattern:
● Model: Data and business logic (examples: TableModel, ListModel)
● View: Visual representation (the Swing components themselves)
● Controller: Handles user input (listeners and event handlers)
Conclusion
Java Swing provides a powerful and flexible toolkit for building desktop applications. By
understanding its class hierarchy, utilizing various containers and layout managers, and
implementing event handling, developers can create responsive and well-structured UIs.
The key to mastering Swing is:
● Knowing how components relate in the class structure
● Organizing them with layout managers
● Reacting to user input through event listeners