Java Module 03
Java Module 03
Q.1.a. "Explain the four types of the Swing buttons, with demonstration program."
Swing provides different types of buttons, all derived from AbstractButton. The four most
common types are:
1. JButton
o A basic push button that performs an action when clicked.
o Commonly used for submitting forms, triggering events, etc.
2. JCheckBox
o Represents a box that can be checked or unchecked.
o Used when multiple selections are allowed.
3. JRadioButton
o Similar to checkboxes, but grouped using ButtonGroup to allow only one
selection at a time.
o Common in forms with mutually exclusive options (e.g., Gender: Male/Female).
4. JToggleButton
o A two-state button that stays pressed or unpressed.
o Used for on/off switches or toggle states.
Demonstration Program:
java
CopyEdit
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
// 1. JButton
JButton btn = new JButton("Click Me");
btn.addActionListener(e -> JOptionPane.showMessageDialog(frame,
"JButton Clicked"));
// 2. JCheckBox
JCheckBox checkBox = new JCheckBox("I Agree");
// 3. JRadioButton
JRadioButton option1 = new JRadioButton("Option 1");
JRadioButton option2 = new JRadioButton("Option 2");
ButtonGroup group = new ButtonGroup();
Java module-03
group.add(option1);
group.add(option2);
// 4. JToggleButton
JToggleButton toggle = new JToggleButton("Toggle");
// Add to Frame
frame.add(btn);
frame.add(checkBox);
frame.add(option1);
frame.add(option2);
frame.add(toggle);
frame.setVisible(true);
}
}
MVC stands for Model-View-Controller. It's a design pattern used in GUI and web applications
to separate logic, UI, and user interaction. The connector architecture defines how these
components interact.
Components of MVC:
1. Model
o Manages the data and business logic.
o Notifies the view when data changes.
2. View
Responsible for displaying the data (UI).
o
It listens to the model and updates when notified.
o
3. Controller
o Handles user input and updates the model accordingly.
o Acts as an intermediary between view and model.
Java module-03
Connector Flow:
sql
CopyEdit
User → Controller → Model → View
Example in Swing:
In Java Swing:
• Swing components can change their appearance without changing the code logic.
• Example: You can switch between Windows, Motif, or Metal look and feel at runtime.
• This makes Swing flexible and adaptable across platforms.
2. Lightweight Components:
Summary:
These features make Swing platform-independent, visually customizable, and better suited for
modern GUIs compared to AWT.
Java module-03
Q.2.a. Explain the following:
(i) JLabel and ImageIcon
(ii) JTextField
JLabel:
Important methods:
ImageIcon:
Example:
java
CopyEdit
JLabel label = new JLabel("Welcome");
ImageIcon icon = new ImageIcon("logo.png");
label.setIcon(icon);
(ii) JTextField:
• JTextField is a Swing component used to accept single-line text input from users.
• It is a key input component in forms and search bars.
Common features:
• Editable by default.
• Can be used with event listeners like ActionListener.
Java module-03
Important methods:
Example:
java
CopyEdit
JTextField tf = new JTextField(20);
panel.add(tf);
String input = tf.getText();
Summary Table:
• JFrame
• JLabel
• JButton
• JTextField
• ActionListener
java
CopyEdit
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
// Display frame
frame.setVisible(true);
}
}
Explanation:
// JLabel
JLabel label = new JLabel("Enter your city:");
// JTextField
JTextField textField = new JTextField(15);
// JButton
JButton button = new JButton("Submit");
// Output Label
JLabel output = new JLabel();
// Add components
frame.add(label);
frame.add(textField);
frame.add(button);
frame.add(output);
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
Java module-03
} catch (Exception e) {
System.out.println("Look and feel not set.");
}
frame.setVisible(true);
}
}
Q.3.b. "Write a program to demonstrate icons representing timepieces using JButton and
JToggleButton. When the button is pressed, the name of that timepiece should appear in a
JLabel."
You can use any timepiece icons like: clock.png, watch.png, timer.png, alarm.png.
Java Program:
java
CopyEdit
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
frame.setVisible(true);
}
}
Q.4.a. Write a program to create a frame for a simple arithmetic calculator using swing
components and layout mangers.
// Create Components
JLabel label1 = new JLabel("Number 1:");
JTextField field1 = new JTextField();
// Action Listeners
addBtn.addActionListener(e -> {
double a = Double.parseDouble(field1.getText());
double b = Double.parseDouble(field2.getText());
resultField.setText(String.valueOf(a + b));
});
subBtn.addActionListener(e -> {
double a = Double.parseDouble(field1.getText());
double b = Double.parseDouble(field2.getText());
resultField.setText(String.valueOf(a - b));
});
mulBtn.addActionListener(e -> {
double a = Double.parseDouble(field1.getText());
double b = Double.parseDouble(field2.getText());
resultField.setText(String.valueOf(a * b));
});
divBtn.addActionListener(e -> {
double a = Double.parseDouble(field1.getText());
double b = Double.parseDouble(field2.getText());
if (b != 0)
resultField.setText(String.valueOf(a / b));
else
resultField.setText("Cannot divide by zero");
});
frame.setVisible(true);
}
}
Q.4.b. Explain the event handling mechanism used by Swing with an example program.
• Clicking a button
• Typing in a text field
• Selecting from a combo box, etc.
1. Event Source – the component that generates the event (e.g., JButton)
2. Event Object – contains information about the event (ActionEvent, MouseEvent, etc.)
3. Event Listener – interface that receives and handles the event (ActionListener,
MouseListener, etc.)
How it works:
// Create Components
JLabel label = new JLabel("Click the button:");
JButton button = new JButton("Click Me");
// Register ActionListener
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
label.setText("Button was clicked!");
}
});
// Add components
frame.add(label);
frame.add(button);
// Show frame
frame.setVisible(true);
}
}
Explanation: