DELHI PUBLIC SCHOOL, BUDGAM
(STUDY MATERIAL – UNIT 1)
(Fundamentals of Java)
Components in Swing
Swing = set of components for creating GUI applications in Java.
Includes: buttons, checkboxes, radio buttons, list boxes, etc.
Each component is an object from a built-in Java class.
Components have: properties, methods, events.
Two types:
1. Container Components
2. Child Components
Container Components
Used to hold and organize child components.
Examples: Frame, Panel, Tabbed Pane, Split Pane, Scroll Pane,
Tool Bar, Desktop Pane, Internal Frame, Layered Pane.
Frame (JFrame)
Top-level container (window with title bar & border).
Holds other components.
Implemented as JFrame class.
Properties:
COMPILED BY MS. AZKA BAZAZ 1
defaultCloseOperation → Action on close (EXIT_ON_CLOSE,
DO_NOTHING, HIDE, DISPOSE).
title → Title of the window (no default value).
Panel (JPanel)
Lightweight container (cannot exist alone).
Must be added inside another container like Frame.
Swing Child Components
Placed inside container components.
Examples: Button, Text Field, Radio Button, Check Box, Text
Area, Label.
Label (JLabel)
• Displays short text (captions/messages).
• Can show images/icons.
• Text cannot be edited at runtime.
Text Field (JTextField)
• Lightweight component for typing small amounts of text
(single line).
• Package: [Link]
• Input: User enters one line of text (String).
• Output: Displays entered text.
Key Points:
COMPILED BY MS. AZKA BAZAZ 2
Stores data as String.
To use numeric values, must convert:
1. Concatenation → [Link]("" + num);
2. toString() method → [Link]([Link](num));
Button (JButton)
• Push button that performs an action when clicked.
• Uses: Command buttons, checkboxes, toggle buttons, radio
buttons (all extend AbstractButton).
Text Area (JTextArea)
• Used for entering/displaying multiple lines of [Link]
vertical & horizontal scroll bars.
COMPILED BY MS. AZKA BAZAZ 3
getText():A method used to retrieve text content from GUI
components (Swing, AWT, JavaFX) or from classes like
StringBuilder, JTextField, JTextArea, JLabel, JButton, etc.
It returns the text as a String.
.append(): A method of StringBuilder and StringBuffer classes in
Java.
• It is used to add text (or other data types) to the end of an
existing sequence.
• Unlike + operator (which creates a new object), .append()
modifies the same object → more efficient.
Example 2
COMPILED BY MS. AZKA BAZAZ 4
[Link]():
• A Swing method used to display a simple pop-up dialog box
with a message.
• Belongs to the JOptionPane class ([Link] package).
• Mainly used for information, warnings, errors,
or confirmations.
• SYNTAX:[Link](parentComponen
t, message);
• Parameters:
1. parentComponent → The parent window for the dialog
(can be null).
2. message → The text, string, or object to display.
Example 3:
COMPILED BY MS. AZKA BAZAZ 5
• [Link](): Commonly used for converting form
input (String) → int in Swing applications.
COMPILED BY MS. AZKA BAZAZ 6
• [Link]() is used to convert a String containing
digits into an int (primitive type).
Example 4:
COMPILED BY MS. AZKA BAZAZ 7
Example 5:
COMPILED BY MS. AZKA BAZAZ 8
COMPILED BY MS. AZKA BAZAZ 9