Java Swing GUI

How to Add Row Dynamically in JTable Java

In this tutorial, we are going to see how to add row dynamically in JTable Java. JTable is a flexible swing component, is a subclass of JComponent class, and it can be used to create a table with information displayed in multiple rows and columns. We can dynamically add rows to a JTable when clicking on “+ Add” button.

To add rows dynamically in a JTable, we have used the method addRow().


Java Program to Add Row Dynamically in JTable Java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.LineBorder;
import javax.swing.table.*;

public class DynamicTable 
    private JTextField text1, text2, text3, text4;
    // JTable Header 
    public static final String[] columns = {
        "Name", "Age", "Address"
    // Create the table model
    private DefaultTableModel model = new DefaultTableModel(columns, 0);
    // Create the JTable
    private JTable table = new JTable(model);
    // Create the main panel
    private JPanel mainPanel = new JPanel(new BorderLayout());

    public DynamicTable() 
        //Add button
        JButton addButton = new JButton("+ Add");
        //Clear button
        JButton clearButton = new JButton("Clear");
        //Button panel
        JPanel buttonPanel = new JPanel();
        //Add buttons to panel
        // This code is called when the Add button is clicked.
        addButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
              //Add form data
                   new Object[]{
        // This code is called when the Clear button is clicked.
        clearButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
              //Clear the form
        //Create the JTextFields panel
        JPanel textPanel = new JPanel(new BorderLayout());
        text1 = new JTextField();
        text2 = new JTextField();
        text3 = new JTextField();
        //Add JTextFields to the panel
        textPanel.add(text1, BorderLayout.NORTH);
        textPanel.add(text2, BorderLayout.CENTER);
        textPanel.add(text3, BorderLayout.SOUTH);
        //Add panels and table to the main panel
        mainPanel.add(textPanel, BorderLayout.NORTH);
        mainPanel.add(new JScrollPane(table), BorderLayout.CENTER);
        mainPanel.add(buttonPanel, BorderLayout.SOUTH);

    //Get the main panel
    public JComponent getComponent() {
        return mainPanel;
    // start the application in thread-safe
    public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                JFrame f = new JFrame("Add automatically to JTable");
                 .add(new DynamicTable().getComponent());


mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *