Hi All;

Im just making a simple Desktop app. Ive got a need so that the user could be able to add data to the table dynamically. Ive done implementing the table and also code for adding rows.
Issue is after i add the row it is not displayng in the table. Of course I am addin it to the tableModel and also firing the insert row updated.
The row count shows that it is increased but not siplayed. Any suggestions/help would be highly appreciated

Code initializing the table (Netbeans Used)

jScrollPane1.setName("jScrollPane1"); // NOI18N

        Object[] columnName = {"Name","Age","Address"};
        Object[][] rowData = {{"a","b","c"}};
        tableModel = new DefaultTableModel(columnName,0);
        for (int i = 0; i < columnName.length; i++) {
            Object object = columnName[i];


        for (int i = 0; i < rowData.length; i++) {
            Object[] objects = rowData[i];


The code which is fired on insert

private void insertNewRow(){
        Object[] val = {jTextField1.getText(),jTextField2.getText(),jTextField3.getText()};
        System.out.println("Row Count : "+tableModel.getRowCount());


1) wrong that you used code generated by NetBeans

2) fireTableRowsInserted is job for AbstractTableModel, don't call tshi method directly

3) eeerrrgggggggggggt, use DefaultTableModel

import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;
import javax.swing.border.LineBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;

public class DefaultTableModelDemo {

    public static final String[] COLUMN_NAMES = {
        "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"
    // the table model and the table:
    private DefaultTableModel model = new DefaultTableModel(COLUMN_NAMES, 0);
    private JTable table = new JTable(model);
    private JPanel mainPanel = new JPanel(new BorderLayout());
    private Random random = new Random();

    public DefaultTableModelDemo() {
        JButton addDataButton = new JButton("Add Data");
        JPanel buttonPanel = new JPanel();
        addDataButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
        table = new JTable(model) {

            private static final long serialVersionUID = 1L;
            //  Returning the Class of each column will allow different
            //  renderers to be used based on Class

            public Class getColumnClass(int column) {
                return getValueAt(0, column).getClass();

            public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
                Component c = super.prepareRenderer(renderer, row, column);
                if (isRowSelected(row) && isColumnSelected(column)) {
                    ((JComponent) c).setBorder(new LineBorder(Color.red));
                return c;
        mainPanel.add(new JScrollPane(table), BorderLayout.CENTER);
        mainPanel.add(buttonPanel, BorderLayout.SOUTH);

    // This code is called when the button is pushed.
    private void addDataActionPerformed() {
        for (int i = 0; i < 5; i++) {
            // creates a row array
            Object[] row = new Object[COLUMN_NAMES.length];
            for (int j = 0; j < row.length; j++) {
                // inserts random Integers into the row
                row[j] = random.nextInt(100);
            // then adds the row to the DefaultTableModel

    public JComponent getComponent() {
        return mainPanel;

    // start my app in a thread-safe way
    public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                JFrame frame = new JFrame("DefaultTableModelDemo");
                frame.getContentPane().add(new DefaultTableModelDemo().getComponent());

Thanks mKorbel for your help...........

Also this did the trick and worked

private void insertNewRow(){
        Object[] val = {jTextField1.getText(),jTextField2.getText(),jTextField3.getText()};
        DefaultTableModel defaultTableModel = (DefaultTableModel) jTable1.getModel();

This question has already been answered. Start a new discussion instead.