Java Programming Practical:22 Lab Manual (Solved)
Resource required (additional)
Name of Resource Broad Specification Remark if any
Java Development Kit Required for Java compilation and
Java 17.0.4
(JDK) execution
Visual Studio Code Lightweight IDE with Simplifies Java coding with syntax
(VS Code) extensions for Java highlighting, debugging, and code
development suggestions
Conclusion
In this practical, we wrote a program to handle text events on Swing components. We learned
how to capture and respond to text input in components like text fields and text areas.
Practical related Questions
1. Which component can be used to accept the multiline input from user.
The JTextArea component in Java Swing is used to accept multiline input from the user.
Key Features of JTextArea:
✔ Allows users to enter multiple lines of text.
✔ Supports copy, paste, and edit operations.
✔ Can be placed inside a JScrollPane for scrolling.
2. Which class is the Swing version of a text field?
JTextField class is used in Swing.
Key Features of JTextField:
✔ Used to accept single-line text input.
✔ Supports copy, paste, and edit operations.
✔ Can generate action events when the user presses Enter.
• JTextField → For single-line input
• JTextArea → For multi-line input
Jamia Polytechnic Akkalkuwa Page No:1 Prepared by: Sayyed Waliullah
Java Programming Practical:22 Lab Manual (Solved)
3. What is the difference between Swing text field and text area?
Difference Between Swing JTextField and JTextArea
Feature JTextField JTextArea
Purpose Used for single-line text input Used for multi-line text input
Class JTextField JTextArea
Supports Line No, only one line is allowed Yes, multiple lines are supported
Breaks?
Scrollbars No built-in scrollbars Supports scrollbars using
JScrollPane
Events Can trigger action events (Enter Does not trigger action events
key press)
Use Case For names, emails, phone For comments, descriptions,
numbers feedback
4. Write a program using JTextField to perform the addition of two numbers.
import javax.swing.*;
import java.awt.event.*;
public class Add {
public static void main(String[] args) {
JFrame frame = new JFrame("Jamia Polytechnic");
frame.setSize(300, 200);
frame.setLayout(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel label1 = new JLabel("Number 1:");
label1.setBounds(30, 20, 80, 30);
JTextField text1 = new JTextField();
Jamia Polytechnic Akkalkuwa Page No:2 Prepared by: Sayyed Waliullah
Java Programming Practical:22 Lab Manual (Solved)
text1.setBounds(120, 20, 100, 30);
JLabel label2 = new JLabel("Number 2:");
label2.setBounds(30, 60, 80, 30);
JTextField text2 = new JTextField();
text2.setBounds(120, 60, 100, 30);
JButton addButton = new JButton("Add");
addButton.setBounds(80, 100, 80, 30);
JLabel resultLabel = new JLabel("Result: ");
resultLabel.setBounds(30, 140, 200, 30);
addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int num1 = Integer.parseInt(text1.getText());
int num2 = Integer.parseInt(text2.getText());
int sum = num1 + num2;
resultLabel.setText("Result: " + sum);
}
});
frame.add(label1);
frame.add(text1);
frame.add(label2);
frame.add(text2);
frame.add(addButton);
frame.add(resultLabel);
frame.setVisible(true);
}
Jamia Polytechnic Akkalkuwa Page No:3 Prepared by: Sayyed Waliullah