Create and use Java Table in NetBeans

Introduction to Java Table (jTable)

Whenever you display to display data in an organized form, most of the time tabular form is preferred. All the languages provide their table libraries which allow selection and many other features. Java Table can be created using JTable component. In JTable, there are multiple features you can add to your table like selecting rows, selecting columns to get content from a particular cell and much more. JTable is Java Swing component. It can implement TableModelListener, Scrollable, TableColumnModelListener, ListSelectionListener, CellEditorListener, Accessible, RowSorterListener. JTable is placed in JScrollPane, as we need the scrollbar whenever the content increases. JScrollPane automatically adjusts the content length and width and place the scroll bar as required.

Components of JTable

  1. Header: Contains the labels of all the columns. Each label tells about the column.
  2. Columns: Each column contains similar content/type of data.
  3. Cells: One single cell contains content/type of data according to cell’s column.

In this tutorial, you will learn how to create and use Java table (JTable), and you will also be able to add rows and columns dynamically to Java table after understanding this tutorial.

Create a new project in Netbeans and add a new JFrame form from File -> New File -> Swing GUI Forms and Select JFrame Form.

New JFrame Form
New JFrame Form


Creating a Table

NetBeans provides a palette from which you can drag and drop user interface elements.

Drag and Drop Table from Palette
Drag and Drop Table from Palette

right click on the table, click on Table Contents.Customizer Dialog of NetBeans will open.

Customizer Dialog in NetBeans
Customizer Dialog in NetBeans

Open Columns Tab, double click on the title cell which you want to rename.

In the Rows Tab, you can perform row operations.

Press Close.


Create Custom Table through Code

You can also do it manually by using JPanel, JScrollPane, and JTable. You should first declare private variables in your class. If you are working on a console application, you must import AWT and Swing components in your project.

Then create private fields in your class, NetBeans automatically import required libraries when you apply fixes of the errors from the editor.

In the constructor of the table set title, size, and background color of the frame.

Set the model of the table. In this example, DefaultTableModel is used.

You have successfully created a table. Now you should add the table header which contains the labels so for that, declare a string array and place the column names at each location.

Create a new Default table model and set the column names i.e. header from the string array.

To add simple rows, use default table model and add rows using a string type array.

Set the model of the table from the default table model created before, add the table to JScrollPane and JScrollPane to JFrame. Make the table visible in the constructor.

In the main method call the instance of the same class.

Complete Code of Hand Coded Table

Use the table for two or more purposes

As I created the table through Default Table Model, I can also use the same code to modify the table completely as required.

Dynamically Add Rows to Java Table (JTable)

Default table model provides a feature to add columns to Java Table, and you can also rename the headings as well. If you want to recreate a table you can also use DefaultTableModel.

To use DefaultTableModel, you should import it in your project.

Create an instance of DefaultTableModel and tell the model about the previous table at the time of creating instance/object.

Add a new row to the object of DefaultTableModel.

You can also place content in your newly added row.

Set the model of the table to DefaultTableModel.

Complete Code of Adding a new Row in JTable

You can use this code in any method, the only thing you need is to import DefaultTableModel in your Project.


Populating a Table in Java

Each cell in of the table in Java is indexed by its row and column as in two-dimensional arrays. The first index of the first row is at column number 0, and row number 0. To set some string on the table, get the model of the table and set value at [0,0] as you set value in two-dimensional arrays.

You can use loops if the data is already present in some other source like dataset or ArrayList. The first index specifies the row index and second index specifies the column index.

populating row and column of Jtable
Populating row and column of Jtable


if you want to populate the table on button click, you can add this code in the button handler.

Complete Button Handler Code To Populate table on Button Click

Download NetBeans Table Source Code (Drag and Drop)

Download Hand Coded Table Source Code