What is Java GUI?
Java GUI allows users to interact with Java programs visually using windows, buttons,
text fields, menus, dialogs, etc., instead of command-line input/output.
Java provides GUI support mainly through:
1. AWT (Abstract Window Toolkit)
2. Swing
3. JavaFX (modern)
AWT (Abstract Window Toolkit)
1 Overview
Oldest Java GUI library
Package: [Link]
Platform-dependent (uses OS native components)
2 Features
Heavyweight components
Faster rendering
Limited customization
3 Common AWT Components
Component Description
Frame Main window
Button Clickable button
Label Displays text
TextField Single line input
TextArea Multi-line input
Checkbox Yes/No selection
Component Description
Choice Drop-down list
List Scrollable list
4 Example (AWT)
import [Link].*;
class MyAWT {
public static void main(String[] args) {
Frame f = new Frame("AWT Example");
Button b = new Button("Click Me");
[Link](100, 100, 80, 30);
[Link](b);
[Link](300, 300);
[Link](null);
[Link](true);
}
Swing
1 Overview
Built on AWT
Package: [Link]
Platform-independent
Most widely used in academics
2 Features
Lightweight components
Rich UI controls
MVC architecture
Pluggable Look & Feel
3 Swing Components
Swing AWT Equivalent
JFrame Frame
JButton Button
JLabel Label
JTextField TextField
JTextArea TextArea
JCheckBox Checkbox
JComboBox Choice
JList List
JTable Table
JMenuBar Menu bar
4 Swing Example
import [Link].*;
class MySwing {
public static void main(String[] args) {
JFrame f = new JFrame("Swing Example");
JButton b = new JButton("Click");
[Link](100, 100, 80, 30);
[Link](b);
[Link](300, 300);
[Link](null);
[Link](true);
[Link](JFrame.EXIT_ON_CLOSE);
Layout Managers
Layout Managers control component positioning automatically.
Types of Layouts
Layout Description
FlowLayout Left to right
BorderLayout North, South, East, West, Center
GridLayout Rows and columns
GridBagLayout Flexible grid
Layout Description
CardLayout Multiple screens
Example (FlowLayout)
[Link](new FlowLayout());
Event Handling in Java GUI
What is Event?
An event is an action like button click, key press, mouse movement.
Event Handling Mechanism
1. Event Source (Button)
2. Event Object
3. Event Listener
Common Listener Interfaces
Listener Event
ActionListener Button click
MouseListener Mouse events
KeyListener Keyboard input
WindowListener Window events
Example (Button Click Event)
import [Link].*;
import [Link].*;
class EventDemo {
public static void main(String[] args) {
JFrame f = new JFrame("Event Handling");
JButton b = new JButton("Click Me");
[Link](100, 100, 100, 30);
[Link](new ActionListener() {
public void actionPerformed(ActionEvent e) {
[Link](f, "Button Clicked");
}
});
[Link](b);
[Link](300, 300);
[Link](null);
[Link](true);
[Link](JFrame.EXIT_ON_CLOSE);
Dialog Boxes (Swing)
Dialog Purpose
JOptionPane Message / Input
JDialog Custom dialog
Example
[Link](null, "Hello Java GUI");
JavaFX
1 Overview
Modern GUI framework
Uses CSS styling
Supports multimedia and animations
2 Simple JavaFX Example
import [Link];
import [Link];
import [Link];
import [Link];
public class FXDemo extends Application {
public void start(Stage stage) {
Button b = new Button("Click");
Scene s = new Scene(b, 300, 200);
[Link](s);
[Link]();
}
public static void main(String[] args) {
launch(args);
}
Difference: AWT vs Swing vs JavaFX
Feature AWT Swing JavaFX
Platform Dependent Independent Independent
Look Native Custom Modern
Performance Fast Moderate Best
Use Today Rare Academic Industry
Advantages of Java GUI
User-friendly
Platform independent
Rich controls
Event-driven programming
AWT (Abstract Window Toolkit) in Java
AWT (Abstract Window Toolkit) is Java’s first GUI library, used to develop window-
based applications such as frames, buttons, text fields, menus, and dialogs.
Package: [Link]
Principle: Uses native operating system components
Why is it called “Abstract Window Toolkit”?
Abstract → Provides abstract classes & interfaces
Window → Used to create windows (Frame, Dialog)
Toolkit → Set of GUI tools (buttons, labels, etc.)
AWT Architecture (Peer Model)
AWT follows the Peer Architecture.
How it works:
Java component → calls Peer
Peer → calls native OS component
Example:
Button → ButtonPeer → Windows Button / Linux Button
Result:
Native look
Platform dependent
Heavy memory usage
Features of AWT
Feature Explanation
Platform Dependent Uses OS native GUI
Heavyweight Components Each component has OS resource
Faster Rendering Native components
Limited Components Few GUI controls
Poor Customization Depends on OS
AWT Component Hierarchy
Object
└── Component
├── Button
├── Label
├── TextField
├── TextArea
├── Checkbox
├── Choice
├── List
└── Container
├── Panel
├── Window
├── Frame
└── Dialog
AWT Components (in detail)
1 Component Class
Base class for all GUI components
Provides methods:
o setSize()
o setBounds()
o setVisible()
o setBackground()
2 Container
A container holds other components.
Types:
1. Panel
2. Window
3. Frame
4. Dialog
3 Frame
Top-level window
Has title bar, close/minimize buttons
Frame f = new Frame("My Frame");
[Link](300, 200);
[Link](true);
4 Panel
Does not have title bar
Used inside Frame
Panel p = new Panel();
5 Button
Clickable component
Button b = new Button("Submit");
6 Label
Displays non-editable text
Label l = new Label("Username:");
7 TextField
Single line input
TextField tf = new TextField(20);
8 TextArea
Multi-line input
TextArea ta = new TextArea(5, 20);
9 Checkbox
On/Off selection
Checkbox cb = new Checkbox("Java");
10 CheckboxGroup (Radio Button)
CheckboxGroup cg = new CheckboxGroup();
Checkbox m = new Checkbox("Male", cg, false);
Checkbox f = new Checkbox("Female", cg, true);
11 Choice (Dropdown)
Choice c = new Choice();
[Link]("BCA");
[Link]("BSc CSIT");
12 List
Scrollable list
List l = new List(3);
[Link]("Java");
[Link]("Python");
7. Layout Managers in AWT
Layout Manager controls component placement.
1 FlowLayout (Default)
Left to right
[Link](new FlowLayout());
2 BorderLayout (Default for Frame)
North
South
East
West
Center
[Link](b1, [Link]);
3 GridLayout
Rows × Columns
[Link](new GridLayout(2, 2));
4 CardLayout
Multiple screens
CardLayout cl = new CardLayout();
5 GridBagLayout (Advanced)
Flexible grid (rare in exams)
Event Handling in AWT
1 What is Event?
An event is an action like:
Button click
Mouse move
Key press
2 Event Delegation Model
3 Components:
1. Event Source (Button)
2. Event Listener
3. Event Object
Common Event Classes
Event Class Description
ActionEvent Button click
Event Class Description
MouseEvent Mouse action
KeyEvent Keyboard input
WindowEvent Window action
Listener Interfaces
Listener Used For
ActionListener Button
MouseListener Mouse
KeyListener Keyboard
WindowListener Window
Example: Button Click Event (AWT)
import [Link].*;
import [Link].*;
class AWTEventDemo extends Frame implements ActionListener {
Button b;
AWTEventDemo() {
b = new Button("Click Me");
add(b);
[Link](this);
setSize(300, 200);
setLayout(new FlowLayout());
setVisible(true);
public void actionPerformed(ActionEvent e) {
[Link]("Button Clicked");
}
public static void main(String[] args) {
new AWTEventDemo();
Window Closing in AWT
AWT does NOT close window automatically.
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
});
Advantages of AWT
Native look
Fast execution
Simple API
Disadvantages of AWT
Platform dependent
Heavyweight
Less components
Poor UI control
Java Swing –
Java Swing is a GUI (Graphical User Interface) toolkit used to create window-based
desktop applications in Java.
Package: [Link]
Built on: AWT
Goal: Rich, platform-independent GUI
Why Swing?
Problems with AWT:
Platform dependent
Heavyweight components
Limited UI controls
Swing solves these problems
Features of Swing
Feature Description
Platform Independent Written in Java
Lightweight Components No native OS dependency
Rich Components JTable, JTree, JTabbedPane
MVC Architecture Model-View-Controller
Pluggable Look & Feel Change UI appearance
Customizable Colors, fonts, borders
Swing Architecture (MVC)
Swing follows MVC pattern:
Part Role
Model Data (JTableModel)
View Display
Controller User input
Example:
JButton → Model (state) → View (appearance) → Controller (action)
Swing Component Hierarchy
Object
└── Component
└── Container
└── JComponent
├── JButton
├── JLabel
├── JTextField
├── JTextArea
├── JCheckBox
├── JComboBox
├── JList
├── JTable
├── JMenu
├── JTabbedPane
Top-Level Containers
Container Description
JFrame Main window
JDialog Popup window
JApplet Browser (obsolete)
JWindow Borderless window
Common Swing Components (Detailed)
1 JFrame
JFrame f = new JFrame("My Frame");
[Link](400, 300);
[Link](true);
[Link](JFrame.EXIT_ON_CLOSE);
2 JLabel
JLabel l = new JLabel("Username");
3 JButton
JButton b = new JButton("Login");
4 JTextField
JTextField tf = new JTextField(20);
5 JPasswordField
JPasswordField pf = new JPasswordField(20);
6 JTextArea
JTextArea ta = new JTextArea(5, 20);
7 JCheckBox
JCheckBox cb = new JCheckBox("Java");
8 JRadioButton
JRadioButton m = new JRadioButton("Male");
JRadioButton f = new JRadioButton("Female");
ButtonGroup bg = new ButtonGroup();
[Link](m); [Link](f);
9 JComboBox
String[] course = {"BCA", "BSc CSIT"};
JComboBox cb = new JComboBox(course);
10 JList
String[] lang = {"Java", "Python"};
JList list = new JList(lang);
11 JTable
String[][] data = {{"1","Ram"},{"2","Shyam"}};
String[] col = {"ID","Name"};
JTable table = new JTable(data, col);
12 JMenuBar
JMenuBar mb = new JMenuBar();
JMenu m = new JMenu("File");
JMenuItem i = new JMenuItem("Exit");
[Link](i);
[Link](m);
[Link](mb);
Layout Managers in Swing
Layout Description
FlowLayout Left to right
BorderLayout 5 regions
GridLayout Row × Column
BoxLayout Vertical / Horizontal
GroupLayout Advanced
Example (GridLayout)
[Link](new GridLayout(2,2));
Event Handling in Swing
Swing uses Event Delegation Model.
Components:
1. Event Source
2. Event Object
3. Event Listener
Common Listeners
Listener Used For
ActionListener Button
MouseListener Mouse
KeyListener Keyboard
ItemListener Checkbox
WindowListener Window
Example: Button Click Event
import [Link].*;
import [Link].*;
class SwingEvent {
public static void main(String[] args) {
JFrame f = new JFrame("Event Demo");
JButton b = new JButton("Click");
[Link](100,100,80,30);
[Link](new ActionListener() {
public void actionPerformed(ActionEvent e) {
[Link](f,"Button Clicked");
});
[Link](b);
[Link](300,300);
[Link](null);
[Link](true);
[Link](JFrame.EXIT_ON_CLOSE);
Dialog Boxes in Swing
Dialog Purpose
JOptionPane Message / Input
JDialog Custom dialog
[Link](null,"Welcome");
Look and Feel
[Link]([Link]());
Advantages of Swing
Platform independent
Rich UI components
Customizable
MVC architecture
Disadvantages of Swing
Slower than AWT
Old compared to JavaFX
Complex for large apps
JavaFX
JavaFX is a modern GUI framework used to build rich desktop applications in Java.
It is the successor of Swing and supports CSS styling, multimedia, animation, and
responsive UI.
Package: javafx.*
Used for: Desktop applications, dashboards, media apps
Why JavaFX?
Limitations of Swing:
Old look
No CSS
Weak multimedia support
JavaFX solves these problems with:
Modern UI
Hardware acceleration
CSS & animation
Features of JavaFX
Feature Description
Modern UI Flat & responsive
CSS Styling Web-like design
FXML Support Separate UI from logic
Multimedia Audio & video
Animation Transitions & effects
Scene Graph Hierarchical UI
Hardware Accelerated Better performance
JavaFX Architecture
JavaFX follows Scene Graph architecture.
Stage (Window)
└── Scene
└── Root Node
├── Controls
├── Layouts
└── Shapes
Key Terms
Stage → Main window
Scene → Container for UI
Node → Every UI element
JavaFX Application Life Cycle
JavaFX programs extend Application.
Life Cycle Methods
Method Purpose
init() Initialization
start(Stage) UI creation
stop() Cleanup
Basic JavaFX Program
import [Link];
import [Link];
import [Link];
import [Link];
public class HelloFX extends Application {
public void start(Stage stage) {
Button b = new Button("Click Me");
Scene scene = new Scene(b, 300, 200);
[Link]("JavaFX Example");
[Link](scene);
[Link]();
public static void main(String[] args) {
launch(args);
JavaFX Controls (Components)
Common Controls
Control Description
Button Click button
Label Text display
TextField Single-line input
PasswordField Password input
TextArea Multi-line input
CheckBox On/Off
RadioButton Single selection
ComboBox Dropdown
ListView List
TableView Table
DatePicker Calendar
Example: Form Controls
TextField tf = new TextField();
PasswordField pf = new PasswordField();
Button login = new Button("Login");
Layout Panes in JavaFX
Layouts automatically arrange components.
Layout Pane Description
VBox Vertical
HBox Horizontal
BorderPane Top, Bottom, Left, Right, Center
Layout Pane Description
GridPane Row × Column
StackPane One over another
AnchorPane Fixed position
Example: GridPane
GridPane gp = new GridPane();
[Link](new Label("Name"), 0, 0);
[Link](new TextField(), 1, 0);
Event Handling in JavaFX
JavaFX uses lambda expressions.
Example: Button Click
Button b = new Button("Click");
[Link](e -> {
[Link]("Button Clicked");
});
Dialogs & Alerts
Alert alert = new Alert([Link]);
[Link]("Info");
[Link]("Hello JavaFX");
[Link]();
Styling with CSS
Inline CSS
[Link]("-fx-background-color: blue; -fx-text-fill: white;");
External CSS
.button {
-fx-font-size: 14px;
-fx-background-color: green;
FXML (UI Separation)
FXML separates design and logic.
UI → .fxml
Logic → Controller class
Advantages
Clean code
MVC architecture
Easy maintenance
Multimedia in JavaFX
Media media = new Media("file:video.mp4");
MediaPlayer mp = new MediaPlayer(media);
MediaView mv = new MediaView(mp);
JavaFX vs Swing (Exam Table)
Feature JavaFX Swing
UI Modern Old
Feature JavaFX Swing
CSS Yes No
FXML Yes No
Multimedia Strong Weak
Performance Better Moderate
Advantages of JavaFX
Modern UI
Clean separation (FXML)
Multimedia support
Animation & effects
Disadvantages of JavaFX
Extra setup required
Larger size
Less academic usage