How to Use JTable to Display Data

In this tutorial, you will learn how to create table for displaying tabular data using JTable class in Java swing application.

JTable class represents Swing table component. JTable provides rich functionality for managing appearance and behavior of the table component. JTable directly extends JComponent and implements several model listener interfaces such as TableModelListener, TableColumnModelListener, ListSelectionListener… etc. JTable also implements scrollable interface therefore table is usually placed in a JScrollPane.

Each JTable has three models TableModel, TableColumnModel, and ListSelectionModel.

  • TableModel is used to specify how the table’s data is stored and retrieval of this data. JTable’s data is often in two-dimensional structure such as a two-dimension array or a Vector of Vectors.  TableModel also is used to specify how data can be editable in the Table.
  • TableColumnModel is used to manage all TableColumn in term of column selection, column order and margin size.
  • ListSelectionModel allows table to have different mode of selection such as single, single interval and multiple interval selections.

We can create a table by using its constructors as follows:

JTable ConstructorsDescription
JTable()Create an empty table
JTable(int rows, int columns)Create a table with rows and columns empty cell.
JTable(Object[][] data, Object[] heading)Create a table with data specify in two-dimensional array data and column heading heading
JTable(TableModel dm)Create a table with a given TableModel
JTable(TableModel dm, TableColumnModel cm)Create a table with a given TableModel and TableColumnModel.
JTable(TableModel dm,TableColumnModel cm, ListSelectionModel sm)Create a table with a given TableModel, TableColumnModel, and ListSelectionModel.
JTable(Vector data, Vector heading) Create a table with data in vector of Vectors data and column headings headin.

Example of using JTable to display stock quotes

In this example, we will display stock quotes in a table using JTable class.

  • First we specify the column heading in the columns array.
  • Then we use two-dimensional array data to store stock quotes data.
  • Next we create an instance of JTable by passing table data and column heading to the constructor.
  • Finally we place the table JScrollPane and add it to the main frame.

Here is the screenshot of the JTable demo application:

JTable Demo
package jtabledemo1; import javax.swing.*; import java.awt.*; public class Main { public static void main(String[] args) { final JFrame frame = new JFrame("JTable Demo"); String[] columns = {"Code", "Name", "High", "Low", "Close", "Volume", "Change","Change %"}; Object[][] data = { {"MBF", "CITYGROUP", 10.16, 10.16, 10.16, 200, 0.08,0.79}, {"MBL", "BANK OF AMERICA", 12.66, 12.66, 12.66, 6600, 0.13,1.04}, {"MJP", "Morgan Stanley Dean Witter & Co.", 24.97, 24.97, 24.97, 1000, -0.04,-0.16} }; JTable table = new JTable(data, columns); JScrollPane scrollPane = new JScrollPane(table); table.setFillsViewportHeight(true); JLabel lblHeading = new JLabel("Stock Quotes"); lblHeading.setFont(new Font("Arial",Font.TRUETYPE_FONT,24)); frame.getContentPane().setLayout(new BorderLayout()); frame.getContentPane().add(lblHeading,BorderLayout.PAGE_START); frame.getContentPane().add(scrollPane,BorderLayout.CENTER); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(550, 200); frame.setVisible(true); } }
Code language: JavaScript (javascript)