Introduction
The JTable in Java is a class part of the Java Swing components Package. It is used to display or edit two-dimensional data composed of both columns and rows. It is similar to a database or spreadsheet. JTable arranges data in a tabular form.

JTable in Java is used to create a table using the information displayed in multiple rows and columns. When a value is selected from JTable, an event named TableModelEvent is generated and handled using the TableModelListener interface. JTable extends JComponent class.
For example: Say you want to display a list of students belonging to a particular class, including details like roll number, name, address, contact, etc. Then you would represent this data in a tabular format using details as columns and the number of rows depending on the number of students data we have. As shown in the image below:
| Roll No. | Name | Contact | Address |
|---|---|---|---|
| 1 | ABC | 4578956124 | ……. |
| 2 | XYZ | 7845895612 | ……. |
| 3 | PQR | 8659234578 | ……. |
Class Declaration in JTable class
Class declaration include Constructors and methods. So, let's see the class declaration for javax.swing.JTable class.
Commonly used Constructors of JTable class
Constructor | Purpose |
|---|---|
JTable() | An empty cell table is created and initialized with the default data model. |
JTable(Object[][] data, Object []col) | A table is created with the specified name where []col represents the column names. |
JTable(int row, int col) | A table is created of size rows x cols. |
JTable(TableModel tm) | Creates a JTable that is initialized by tm as the data model, default section model, and default column model. |
Commonly used Functions of JTable class
Method | Description |
|---|---|
getModel() | Gets the TableModel whose data is displayed by JTable. |
setValueAt(Object value, int row, int col) | Sets the cell value as ‘value’ for the position row and column in the JTable. |
getRowCount() | Gets the current total number of rows in the JTable. |
addColumn(TableColumn []column) | Adds a column at the end of the JTable. |
getColumns() | Gets the current total number of the columns in the JTable. |
editCellAt(int row, int col) | Edits the intersecting cell of the column number ‘col’ and row number ‘row’ programmatically if the given indices are valid and the corresponding cell is editable. |
setDefaultRenderer(Class <?>class,TableCellRenderer renderer) | Sets the default cell renderer to be used to set the values, alignment, background of a cell in JTable. |





