Monday, 2 February 2026

Java ArrayList handling Employee details like adding, listing and searching using Swing widget

An ArrayList in Java is a dynamic and resizable array declared in java.util package. Unlike standard array whose size is fixed, the ArrayList contents can dynamically grow and shrink. Following is a simple example which illustrates a menu driven program defining Arraylist methods using Swing GUI widget.

Swing is a lightweight GUI widget toolkit for Java and also used for building optimized window based applications, it provides a range of components like(JButton, JTextField, JTextArea, JFrame etc).

Below is a simple program having Employee object and handling it using ArrayList with Swing GUI toolkit with operations like .

 

basic add operation




list all operation


search operation


import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

class Employee {
long id;
String name;
double basic;
public Employee(long id, String name, double basic) {
super();
this.id = id;
this.name = name;
this.basic = basic;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBasic() {
return basic;
}
public void setBasic(double basic) {
this.basic = basic;
}
}


public class EmployeeHandlerSwing extends JFrame {
private ArrayList<Employee> empList = new ArrayList<Employee>();
JLabel idLabel;
JTextField idField;
JLabel nameLabel;
JTextField nameField;
JLabel basicSalaryLabel;
JTextField basicSalaryField;
JButton addButton;
JButton listAllButton;
JTextArea ta;
JButton searchButton;
JTextField searchTextField;
public EmployeeHandlerSwing() {
setBounds(300, 90, 350, 600);
setLayout(null);
idLabel = new JLabel("ID");
idLabel.setBounds(80, 60, 70, 40);
add(idLabel);
idField = new JTextField("");
idField.setBounds(140, 60, 100, 30);
add(idField);
nameLabel = new JLabel("Name");
nameLabel.setBounds(80, 95, 70, 40);
add(nameLabel);
nameField = new JTextField("");
nameField.setBounds(140, 100, 100, 30);
add(nameField);
basicSalaryLabel = new JLabel("Basic");
basicSalaryLabel.setBounds(80, 130, 70, 40);
add(basicSalaryLabel);
basicSalaryField = new JTextField("");
basicSalaryField.setBounds(140, 140, 100, 30);
add(basicSalaryField);
addButton = new JButton("Add");
addButton.setBounds(80, 190, 70, 40);
add(addButton);
listAllButton = new JButton("List All");
listAllButton.setBounds(190, 190, 100, 40);
add(listAllButton);
ta = new JTextArea();
ta.setBounds(80, 250, 200, 170);
add(ta);
searchButton = new JButton("Search");
searchButton.setBounds(80, 460, 100, 40);
add(searchButton);
searchTextField = new JTextField("");
searchTextField.setBounds(190, 460, 100, 40);
add(searchTextField);
addButton.addActionListener(e -> {
Long id = Long.parseLong(idField.getText());
String name = nameField.getText();
double basicSal = Double.parseDouble(basicSalaryField.getText());
    
    System.out.println("Adding: " + id + " - " + name + " - " + basicSal);
    
    Employee emp = new Employee(id, name, basicSal);
    empList.add(emp);
    
    ta.setText(emp.getId() + " " + emp.getName() + " " + emp.getBasic());
});
listAllButton.addActionListener(e -> {
ta.setText("");
empList.forEach(emp -> {
ta.append(emp.getId() + " " + emp.getName() + " " + emp.getBasic() + System.getProperty("line.separator"));
});
});
searchButton.addActionListener(e -> {
ta.setText("");
long searchId = Long.parseLong(searchTextField.getText());
empList.forEach(emp -> {
if(emp.getId() == searchId) {
ta.append(emp.getId() + " " + emp.getName() + " " + emp.getBasic());
}
});
});
setVisible(true);
}

public static void main(String[] args) {
new EmployeeHandlerSwing();

}

}








No comments:

Post a Comment