Add a row with data in a JTable. - Java Swing

Java examples for Swing:JTable Row

Description

Add a row with data in a JTable.

Demo Code


//package com.java2s;

import javax.swing.JTable;

import javax.swing.table.DefaultTableModel;

public class Main {
    /**//  w  ww. ja v  a  2  s.  co  m
     * Add a row with data in a table. When the row does not exist, a new
     * row is created using DefaultTableModel.addRow.
     *
     * @param table
     * @param row
     * @param data
     */
    public static void setTableRow(JTable table, int row, Object[] data) {

        if (table.getRowCount() - 1 >= row) {

            for (int column = 0; column < table.getColumnCount(); column++) {
                table.setValueAt(data[column], row, column);
            }

        } else {
            ((DefaultTableModel) table.getModel()).addRow(data);
        }

    }
}

Related Tutorials