Slip No 23 Q A

Write a Java Program to display the details of College(CID, CName, address, Year)on JTable.

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;

public class CollegeTableDisplay extends JFrame {

    // Constructor
    public CollegeTableDisplay() {
        setTitle("College Details");
        setSize(600, 300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

        // Column Names
        String[] columns = {"CID", "CName", "Address", "Year"};

        // Sample Data (You can replace this with database fetch)
        Object[][] data = {
                {101, "ABC College", "Pune", 1995},
                {102, "XYZ Institute", "Mumbai", 2000},
                {103, "DEF University", "Nagpur", 2010},
                {104, "GHI College", "Nashik", 2005},
                {105, "JKL Institute", "Aurangabad", 2015}
        };

        // Create Table Model
        DefaultTableModel model = new DefaultTableModel(data, columns);

        // Create JTable
        JTable table = new JTable(model);

        // Add JTable to JScrollPane
        JScrollPane scrollPane = new JScrollPane(table);

        // Add ScrollPane to Frame
        add(scrollPane, BorderLayout.CENTER);

        // Make frame visible
        setVisible(true);
    }

    public static void main(String[] args) {
        new CollegeTableDisplay();
    }
}
Spread the love

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top