Unit 2 (Swing, App, Bean)
Unit 2 (Swing, App, Bean)
S.N
O
AWT Swing
The components of Java AWT are The components of Java Swing are light
2.
heavy weighted. weighted.
The execution time of AWT is The execution time of Swing is less than
4.
more than Swing. AWT.
The components of Java AWT are The components of Java Swing are
5.
platform dependent. platform independent.
AWT is a thin layer of code on top Swing is much larger swing also has
9
of the operating system. very much richer functionality.
1. JApplet (Deprecated)
A Swing-based applet, subclass of java.applet.Applet.
GUI is added to the contentPane of the JApplet.
Uses layout managers like FlowLayout, BorderLayout, etc.
Key Methods:
2. JButton
A clickable button that performs an action.
Constructors:
JButton(String text)
JButton(Icon icon)
JButton(String text, Icon icon)
Important Methods:
addActionListener(ActionListener l)
setText(String)
setEnabled(boolean)
setIcon(Icon)
3. JToggleButton
A button that maintains a selected or deselected state.
Constructors:
JToggleButton(String text)
JToggleButton(Icon icon)
JToggleButton(String text, boolean selected)
Useful Methods:
4. JCheckBox
Used to allow multiple independent selections.
Constructors:
JCheckBox(String label)
JCheckBox(String label, boolean selected)
Important Methods:
isSelected()
setSelected(boolean)
5. JRadioButton
Allows only one option selected from a group (use with ButtonGroup).
Constructors:
JRadioButton(String text)
JRadioButton(String text, boolean selected)
🔷 6. JComboBox
A drop-down list for selecting one item (can also allow text input if editable).
Constructors:
Important Methods:
getSelectedItem()
setSelectedItem(Object)
addItem(E item)
removeItem(Object)
setEditable(boolean)
7. JTextField
A single-line editable text field.
Constructors:
JTextField(int columns)
JTextField(String text)
JTextField(String text, int columns)
Common Methods:
getText()
setText(String)
setEditable(boolean)
8. JTextArea
Multi-line text area for input/output.
Constructors:
JTextArea(int rows, int columns)
JTextArea(String text, int rows, int cols)
Common Methods:
getText()
setText(String)
append(String)
Often placed inside JScrollPane.
frame.setSize(400, 300);
frame.setVisible(true);
Summary Table:
1. Icons (ImageIcon)
📌 Definition:
An Icon in Java Swing is an interface that represents a fixed-size picture. The most
commonly used implementation is ImageIcon.
📌 Key Uses:
📌 Common Class:
ImageIcon icon = new ImageIcon("path/to/image.png");
📌 Important Notes:
🔷 2. JLabel
📌 Definition:
JLabel is a Swing component used to display a short string or an image (or both). It is non-
editable and used for labeling input fields or showing static information.
📌 Key Features:
📌 Constructors:
JLabel(String text)
JLabel(Icon image)
JLabel(String text, Icon icon, int alignment)
🔷 3. JTabbedPane
📌 Definition:
JTabbedPane is a component that lets the user switch between a group of components by
clicking on a tab with a given title and/or icon.
📌 Key Features:
📌 Constructors:
JTabbedPane()
JTabbedPane(int tabPlacement)
📌 Methods:
addTab(String title, Component component)
setSelectedIndex(int index)
getSelectedComponent()
🔷 4. JScrollPane
📌 Definition:
📌 Key Features:
📌 Constructor:
JScrollPane(Component view)
📌 Use Case:
🔷 5. JList
📌 Definition:
JList is a Swing component that displays a list of items. Users can select one or more items
from the list.
📌 Key Features:
📌 Constructors:
JList(E[] listData)
JList(ListModel<E>)
📌 Selection Modes:
SINGLE_SELECTION
SINGLE_INTERVAL_SELECTION
MULTIPLE_INTERVAL_SELECTION
🔷 6. JTree
📌 Definition:
JTree is a component that displays hierarchical data, such as directories and files.
📌 Key Features:
📌 Common Classes:
📌 Use Case:
🔷 7. JTable
📌 Definition:
📌 Key Features:
📌 Constructor:
JTable(Object[][] data, Object[] columnNames)
📌 Common Usage:
🔚 Summary
Component Description Purpose/Use
Icon Represents an image Used with buttons, labels, etc.
JLabel Displays non-editable text or images Static text, labels
JTabbedPane Provides tabbed interface Tab navigation between views
JScrollPane Adds scrollbars to components Scrollable containers
JList Displays a list of selectable items Multiple/single item selection
JTree Shows hierarchical tree structure File trees, menus
JTable Displays tabular data Data grids, editable cells
A Java Bean is a reusable software component that can be visually manipulated in builder
tools. It follows a set of design conventions that make it easy to use in GUI-based
development environments.
🔸 JavaBeans are:
Java classes that encapsulate multiple objects into a single object (the bean).
Serializable (can be saved to disk and restored).
Easily manipulated in visual editors (like NetBeans, Eclipse GUI builder).
🔸 JavaBean Requirements:
🔸 How It Works:
🔸 Example:
public class StudentBean {
private String name;
BDK (Bean Development Kit) is a tool provided by Sun Microsystems (now Oracle) that
allows developers to create, test, and manipulate Java Beans in a visual environment.
🔸 Features:
🔸 Main Components:
🔸 Required Components:
🔸 Conceptual Design:
// Header
JLabel header = new JLabel("Welcome to My Home Page",
JLabel.CENTER);
header.setFont(new Font("Arial", Font.BOLD, 18));
add(header, BorderLayout.NORTH);
// Navigation buttons
JPanel navPanel = new JPanel();
navPanel.add(new JButton("Home"));
navPanel.add(new JButton("About"));
navPanel.add(new JButton("Contact"));
add(navPanel, BorderLayout.SOUTH);
// Center content
JTextArea content = new JTextArea("This is a sample home page using
Swing inside an Applet.");
add(new JScrollPane(content), BorderLayout.CENTER);
}
}
⚠️Note: Modern Java versions deprecate applets. This is valid only in Java 8 or older.
🔚 Summary
Topic Description
Reusable components with encapsulated data, following specific
JavaBeans
conventions
Advantages Reusability, encapsulation, GUI support, platform-independence
Introspection Enables GUI tools to analyze beans’ properties, methods, events
BDK Visual tool to create/test JavaBeans, with drag-drop support
Applet & Swing Home Simple GUI with layout, navigation, and content using Swing
Page inside an applet