Monday, 6 November 2023

Write a java Program to accept the details of 5 employees (Eno, Ename, Salary) and display it onto the JTable.

Write a java Program to accept the details of 5 employees (Eno, Ename, Salary) and display it onto the JTable.

import javax.swing.*;

import javax.swing.table.DefaultTableModel;

 

public class EmployeeDetailsApp {

    public static void main(String[] args) {

        JFrame frame = new JFrame("Employee Details");

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.setSize(400, 300);

 

        // Create a table model with column names

        DefaultTableModel tableModel = new DefaultTableModel();

        tableModel.addColumn("Employee Number");

        tableModel.addColumn("Employee Name");

        tableModel.addColumn("Salary");

 

        // Create a JTable with the table model

        JTable employeeTable = new JTable(tableModel);

 

        // Create a scroll pane to host the JTable

        JScrollPane scrollPane = new JScrollPane(employeeTable);

 

        frame.add(scrollPane);

 

        // Add employee details to the JTable

        for (int i = 1; i <= 5; i++) {

            String eno = JOptionPane.showInputDialog("Enter Employee Number " + i);

            String ename = JOptionPane.showInputDialog("Enter Employee Name " + i);

            String salary = JOptionPane.showInputDialog("Enter Salary for Employee " + i);

 

            tableModel.addRow(new String[]{eno, ename, salary});

        }

 

        frame.setVisible(true);

    }

}