Java Swing Basics
Java Swing Basics
Course Outline:
• Topic 1 – Java Swing Overview
• Topic 2 – Java Swing JFrame
• Topic 3 – Java Swing JPanel
• Topic 4 – Java Swing JWindow
• Topic 5 – Java Swing JLabel
• Topic 6 – Java Swing JButton
• Topic 7 – Java Swing JCheckBox
• Topic 8 – Java Swing JTextField
• Topic 9 – Java Swing JTextArea
• Topic 10 – Java Swing JComboBox
• Topic 11 – Java Swing JRadioButton
• Topic 12 – Java Swing JList
• Topic 13 – Java Swing JOptionPane
• Topic 14 – Java Swing JPasswordField
Topic 1
Java Swing Overview
Swing is a powerful and widely used Java GUI (Graphical User Interface)
toolkit that provides a rich set of components to build interactive and
visually appealing applications. Unlike AWT, Swing components are pure
Java-based, which means they are not dependent on the native platform's
GUI components. This allows Swing to offer consistent appearance and
behavior across different operating systems.
1. JFrame: The JFrame class is a top-level container that represents the main application
window. It provides all the features of AWT's Frame but with additional
functionalities.
2. JPanel: The JPanel class is a lightweight container that is often used to group other
components together or create custom components.
3. JLabel: The JLabel class is used to display text or an image. It is commonly used for
headings, descriptions, or icons.
4. JButton: The JButton class is used to create a clickable button that triggers an action
when clicked.
10. JList: The JList class represents a scrollable list of items from
which the user can make multiple selections.
Swing Event Handling and Listener
Interfaces
Just like AWT, Swing also uses event handling to respond to user interactions. The
event handling mechanism in Swing is similar to AWT and involves implementing
various listener interfaces. Here are some commonly used listener interfaces in
Swing:
import javax.swing.JFrame;
frame.setTitle("My JFrame");
frame.setSize(400, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
4. Add components (optional): You can add other Swing components like buttons, labels, etc.,
to the JFrame.
5. Display the JFrame: Finally, make the JFrame visible by setting its visibility to true.
frame.setVisible(true);
Example: Here's a complete example that
creates a simple JFrame with a label and
displays it:
JFrame Properties and Attributes
JFrame provides various properties and attributes that allow you
to control its behavior and appearance. Some commonly used
ones include:
• Title: Sets the title displayed on the title bar of the JFrame.
• Size: Sets the width and height of the JFrame window.
• DefaultCloseOperation: Defines the action to be performed
when the JFrame is closed (e.g., EXIT_ON_CLOSE to exit the
application).
• Resizable: Specifies whether the JFrame can be resized by the
user.
• IconImage: Sets the icon image displayed in the title bar of the
JFrame.
JFrame Layouts and Content Panes
JFrame uses a content pane to hold the GUI components added to it.
Layout managers are used to arrange these components within the content
pane. Layout managers handle how components are positioned and resized
as the window size changes.
import javax.swing.JPanel;
import javax.swing.JWindow;
1. Import necessary packages: Before creating JLabels, import the required Swing
packages.
import javax.swing.JLabel;
2. Create a JLabel object: Create an instance of JLabel with the desired text or
icon using the appropriate constructor.