add Empty Row to JTable - Java Swing

Java examples for Swing:JTable Row

Description

add Empty Row to JTable

Demo Code


import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import javax.swing.DefaultCellEditor;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableColumn;

public class Main{
    public static void addEmptyRow(JTable jTable) {

        DynamicTableModel dynamicModel = (DynamicTableModel) jTable
                .getModel();//from   ww w .ja  va 2  s.  c  o m

        ArrayList<Object> newRow = new ArrayList<Object>();
        //fill with empty data this row
        for (int i = 0; i < dynamicModel.getColumnCount(); i++) {
            newRow.add("");
        }

        //add this row to model
        dynamicModel.getRows().add(newRow);
        //notify this change
        dynamicModel.fireTableRowsInserted(dynamicModel.getRows().size(),
                dynamicModel.getRows().size());
    }
}

Related Tutorials