Advanced Java Technology - Unit II (AWT and Swing)
1. Introduction to AWT
AWT (Abstract Window Toolkit) is the original Java platform for building graphical user interfaces (GUIs). It provides a
rich set of UI components like buttons, checkboxes, text fields, and menus. AWT uses the native GUI components of the
host operating system via a mechanism called 'peers', making AWT components heavyweight. It was the foundation for
Java's GUI programming but had limitations in terms of platform consistency and UI richness.
2. AWT Controls
AWT controls include basic widgets used to interact with the user. These include:
- Label: Displays a non-editable text.
- TextField: Accepts single-line text input.
- Button: Triggers an action when clicked.
- Checkbox: Allows multiple selections.
- RadioButton (CheckboxGroup): Allows single selection.
- Choice: Dropdown list of options.
- List: Displays a list of selectable items.
These controls are added to containers such as Frame or Panel and are positioned using layout managers.
3. Graphics in AWT
Java AWT Graphics is handled by the [Link] class. It is used to draw shapes, text, and images. The
paint(Graphics g) method is overridden in components like Canvas or Panel to implement custom drawing. Graphics
class provides methods like drawLine(), drawRect(), drawOval(), drawString(), and drawImage().
4. Layout Managers and Menus
Layout managers automatically arrange components in a container. Types include:
- FlowLayout: Arranges components in a left-to-right flow.
- BorderLayout: Divides the container into five regions.
- GridLayout: Arranges components in a grid.
Menus in AWT use MenuBar, Menu, and MenuItem classes. A MenuBar is added to a Frame. Submenus and checkable
menu items are also supported.
5. Images in AWT
Images are handled using the [Link] class. Images can be loaded using the Toolkit class. To draw an image,
use [Link](). Images can be displayed with or without scaling. An ImageObserver can be used to track image
loading and rendering.
6. Additional Packages
AWT is supported by several additional packages:
- [Link]: Handles various event types like ActionEvent, MouseEvent, KeyEvent.
- [Link]: Supports image manipulation.
- [Link]: Supports clipboard and drag-drop features.
- [Link]: Enables printing support in Java applications.
Advanced Java Technology - Unit II (AWT and Swing)
7. Java Swing Overview
Swing is a more advanced GUI toolkit that extends AWT. It provides lightweight, platform-independent UI components.
Unlike AWT, Swing components do not rely on native OS peers. Swing includes widgets like JTable, JTree, and
JTextPane. Swing supports MVC architecture, enabling separation of model, view, and controller.
8. Creating Swing Applets and Applications
Swing applications are built using JFrame as the main window. Components are added to the content pane using
getContentPane(). Swing Applets extend JApplet and override the init() method. Though applets are outdated, they are
still part of some legacy systems.
9. Programming Using Panes
Swing provides multiple panes for advanced control over GUI layout:
- RootPane: Contains the base structure.
- ContentPane: Holds visible components.
- GlassPane: Transparent layer for painting or intercepting events.
- LayeredPane: Allows overlapping components in layers.
10. Pluggable Look and Feel
Swing supports pluggable look and feel (PLAF), which allows changing the UI style at runtime. Common themes include
Metal, Nimbus, Motif, and Windows. Use [Link]() and [Link]() to
apply changes.
11. Core Swing Components
Swing provides a wide range of interactive UI elements:
- JLabel: Displays text or images.
- JTextField: Accepts single-line input.
- JButton: Performs actions when clicked.
- JCheckBox and JRadioButton: For multi/single choice options.
- JComboBox: Dropdown menu for selection.
- JList: Lists multiple selectable items.
- JScrollPane: Adds scrollbars to large components.
- JProgressBar: Indicates task completion status.
12. Menus and Toolbars
Swing menus use JMenuBar, JMenu, and JMenuItem to create navigable menu systems. JCheckBoxMenuItem and
JRadioButtonMenuItem add interactivity. Toolbars (JToolBar) contain buttons and controls for quick access to functions.
13. Layered, Tabbed, and Split Panes
- JLayeredPane allows Z-order management (stacking components).
- JTabbedPane organizes components in tabbed pages.
Advanced Java Technology - Unit II (AWT and Swing)
- JSplitPane divides space into resizable sections horizontally or vertically.
14. Layouts
Swing supports AWT layout managers and introduces BoxLayout. BoxLayout arranges components in a single row or
column. Use layout managers to make the UI adaptable to window resizing.
15. Windows and Dialog Boxes
- JFrame: Top-level window with title bar.
- JDialog: Secondary window for messages, forms, etc.
- JOptionPane: Provides standard dialogs like message, confirm, and input boxes.