How do I create a simple JTable component?

The code snippet presented below shows you how to create a simple JTable component in a swing application. To create a JTable component we initializes it using the constructor that accept two parameters.

The first parameter is the table’s row of data which type is Object[][], a two-dimensional array of Object. The second parameter is the table’s column names which type is Object[], an array of object.

After the JTable instance is created we places it inside a scroll pane component which in turn is added to the frame’s content pane.

package org.kodejava.example.swing;

import javax.swing.*;
import java.awt.*;
import java.util.Calendar;
import java.util.Date;

public class SimpleJTableDemo extends JFrame {
    public SimpleJTableDemo() throws HeadlessException {

    private void initializeUI() {
        // Defines table's column names.
        String[] columnNames = {
                "ID", "Name", "Date of Birth", "Sex"
        // Defines table's data.
        Object[][] rowData = {
                {1, "Alice", createDOB(1980, 1, 1), "F"},
                {2, "Bob", createDOB(1982, 6, 21), "M"},
                {3, "Carol", createDOB(1970, 10, 12), "M"},
                {4, "Mallory", createDOB(1988, 2, 19), "M"}

        // Initializes an instance of JTable and specifies the table
        // data and column names. After that we place the table in a
        // scroll pane.
        JTable table = new JTable(rowData, columnNames);
        JScrollPane pane = new JScrollPane(table);

        // Sets the frame setting.
        setTitle("Simple JTable Demo");
        setSize(new Dimension(400, 200));
        setLayout(new BorderLayout());

        getContentPane().add(pane, BorderLayout.CENTER);

    private Date createDOB(int year, int month, int day) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month, day);
        return calendar.getTime();

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new SimpleJTableDemo().setVisible(true);

When we running the program we will see the following result:

Simple JTable Demo


Programmer, runner, recreational diver, live in the island of Bali, Indonesia. Mostly programming in Java, Spring Framework, Hibernate / JPA.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.