In this tutorials, we are going to create a simple JTable with Swing in Java. The Swing JTable is used to display data as a regular two-dimensional table of cells.
Java Swing JTable :
JTableDemo.java
package com.swing.examples;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class JTableDemo extends JFrame {
JTable table;
JTableDemo() {
super("JTable Demo");
String headers[] = { "Name", "Address", "Phone", "Experiance" };
String data[][] = { { "Chandra", "Hyderabad", "4568569568", "true" },
{ "Srikanth", "Vijayawada", "8595652541", "true" },
{ "Rajesh", "Banglore", "8585656545", "false" },
{ "Charan", "Mumbai", "9858654852", "true" },
{ "Kumar", "Pune", "4568569568", "2" },
{ "Venu", "Chennai", "8451265923", "2" },
{ "Gopal", "Vizag", "7845956585", "2" } };
// Creating JTable with table data and headers
table = new JTable(data, headers);
// Adding table to content pane
getContentPane().add(new JScrollPane(table));
setSize(500, 150);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JTableDemo();
}
}
Output :
Happy Learning 🙂