jtable in java
JTable is a component in the Java Swing library that displays data in a tabular format. It provides a way to display and manipulate tabular data with many built-in features such as sorting, filtering, and editing. JTable can be customized by modifying the default rendering and editing mechanisms provided by the Swing library.
Here is a simple example of how to create and display a JTable in Java:
refer to:igiftidea.comimport javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class JTableExample {
public static void main(String[] args) {
// Create data for the table
Object[][] data = {
{"John", "Doe", 25},
{"Jane", "Doe", 27},
{"Bob", "Smith", 30},
{"Tom", "Jones", 32},
{"Sue", "Johnson", 24}
};
// Create column headers
String[] columnNames = {"First Name", "Last Name", "Age"};
// Create JTable with data and column headers
JTable table = new JTable(data, columnNames);
// Create a scroll pane for the table
JScrollPane scrollPane = new JScrollPane(table);
// Create a JFrame and add the scroll pane to it
JFrame frame = new JFrame("JTable Example");
frame.add(scrollPane);
// Set the size and visibility of the JFrame
frame.setSize(400, 200);
frame.setVisible(true);
}
}
This creates a simple JTable with five rows and three columns, and displays it in a JFrame with a scroll pane. The resulting table can be sorted by clicking on the column headers, and the cells can be edited by double-clicking on them.
