Material Unit 5.3
Object-Oriented Programming
Java Swing
Introduction to Swing
Original Java GUI: Composed of Abstract Window Toolkit (AWT), providing necessary support for creating GUI components.
Popularity: AWT became popular after its introduction.
Limitations of AWT:
Limited graphical capabilities.
Strong dependency on the native operating system for rendering components.
Translated AWT components into corresponding native components for display.
Limitations and Developments
Components look and feel is dependent on the native system.
Swing Development: Jointly developed by Sun, Netscape, and other companies; integrated since Java 1.2, forming a major part of the Java Foundation Classes.
Resource Usage: AWT components use resources from the native OS and require significant memory. Swing components, being pure Java, are less dependent on the native system, hence utilize less memory.
Component Types
Heavyweight Components: AWT components are referred to as heavyweight due to their resource demands.
Lightweight Components: Most Swing components are lightweight, built on top of AWT, though some remain heavyweight.
Package: All Swing classes are contained within the
javax.swingpackage.
Look and Feel Customization
Customization: Swing components can have a customizable look and feel, providing flexibility for programmers.
Pluggable Look and Feel (PLAF): Offers standard look and feel classes for consistency in design.
Naming Conventions
All Swing classes begin with the letter "J" to distinguish them from AWT classes.
Example: AWT Button class vs. Swing JButton class, AWT Applet class vs. Swing JApplet class (derived from Applet).
Benefits of using Swing
Larger Set of Components: Swing provides a more extensive set of GUI components compared to AWT.
Memory Efficiency: Most Swing components are lightweight and independent of native operating systems.
Customization: Programmable look and feel options through PLAF.
Hierarchy of Swing Class
Inheritance: Swing classes are derived from corresponding AWT classes, prefix "J" differentiates them.
Component hierarchy:
Container -> Component -> JComponent -> Applet, Frame, Dialog
Key Swing classes derived from JComponent: JApplet, JFrame, JDialog.
Swing Classes Overview
Swing components derive from the Container class, allowing them to utilize container methods.
JFrame can host various components like buttons and text fields.
JButton is itself a container that can display names.
Top Swing GUI Containers
JApplet: Base class for Swing applets supporting other Swing classes.
JComponent: Base class for Swing components like JButton and JCheckBox.
Utilizes subclasses to create application components.
Key Swing GUI Classes
JDialog: Base class for creating dialog windows for data entry.
JFrame: Derived from AWT's Frame, serves as the main window for Swing applications with features like borders, titles, and icons for window management.
JFrame Structure
Compatible with several window panes for component management.
JRootPane: Area below the title bar.
Content Pane: For adding components and managed by layout managers.
Glass Pane: Transparent layer allowing background visibility.
Component Management in JFrame
Common methods of the JFrame class:
setContentPane(Container container): sets the ContentPane for the JFrame.getContentPane(): gets a reference to the ContentPane.setGlassPane(Component component): sets a GlassPane.
Swing Package and Components
Swing package contains over 250 classes and about 40 user interface components.
Examples of Swing components: JButton, JCheckBox, JFrame, JDialog, etc.
Adding Components to Containers
Components are added to containers via
add()method.For JFrame: Create instance, access ContentPane, and add components accordingly.
JPanel
JPanel: General-purpose container component and a subclass of JComponent, allows for background color setting and layout management.
Used for grouping Swing components and can be added to the JFrame's ContentPane.
Adding JPanel and Components
Creating JPanel:
JPanel jpanel = new JPanel();It can have its own layout manager and background settings.
Program Example
Importing Swing package and extending JFrame for custom components. "
import javax.swing.*;""class ApplSwing2 extends JFrame {..}"Basic visibility and setup methods for JFrame.
JLabel
Encapsulates labels displaying text or icons, acts as a passive component for information display.
Constructors to create JLabels with text or icons and set their alignments using SwingConstants.
JLabel Methods
Manage label text and icon states; includes methods for gap spacing and shortcut keys configuration.
Swing Packages
Main package
javax.swing, necessary for using core Swing components in applications.
Program Example (JLabel)
Constructing JLabel and adding to ContentPane:
Create JLabel object providing text and alignment.
Set label properties and add it to JFrame.
Layout of JLabels
setBounds()method sets position and dimension of JLabel components within a container.Use x and y coordinates to define the position relative to the top-left corner.