-
Notifications
You must be signed in to change notification settings - Fork 0
/
10_JTableP.java
30 lines (28 loc) · 1.32 KB
/
10_JTableP.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/*
10. Write a Java program to create a JTable.
*/
import javax.swing.JFrame; // Importing the JFrame class
import javax.swing.JScrollPane; // Importing the JScrollPane class
import javax.swing.JTable; // Importing the JTable class
class JTableP { // Class to create a JTable
JFrame f; // Frame to hold the JTable
JTable j; // JTable to display the data
JTableP() { // Constructor to create the JTable
f = new JFrame(); // Creating a new JFrame
f.setTitle("JTable Program"); // Setting the title of the JFrame
String[][] data = {
{ "Kundan Kumar Jha", "4031", "CSE" },
{ "Anand Jha", "6014", "IT" }
}; // 2D array to store the data
String[] columnNames = { "Name", "Roll Number", "Department" }; // Array to store the column names
j = new JTable(data, columnNames); // Creating a new JTable
j.setBounds(30, 40, 200, 300); // Setting the bounds of the JTable
JScrollPane sp = new JScrollPane(j); // Creating a new JScrollPane to hold the JTable
f.add(sp); // Adding the JScrollPane to the JFrame
f.setSize(500, 200); // Setting the size of the JFrame
f.setVisible(true); // Setting the JFrame to be visible
}
public static void main(String[] args) {
new JTableP(); // Creating a new JTableP object
}
}