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];
            tableModel.addColumn(object);

        }

        for (int i = 0; i < rowData.length; i++) {
            Object[] objects = rowData[i];
            tableModel.addRow(objects);
        }*/
        jTable1.setModel(tableModel);
        jTable1.setFillsViewportHeight(true);
        jTable1.setColumnSelectionAllowed(true);
        jTable1.setName("jTable1");
        jScrollPane1.setViewportView(jTable1);
        jTable1.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);

        jPanel1.add(jScrollPane1);

The code which is fired on insert

private void insertNewRow(){
        Object[] val = {jTextField1.getText(),jTextField2.getText(),jTextField3.getText()};
        
        tableModel.addRow(val);
        
        tableModel.fireTableRowsInserted(tableModel.getRowCount(), 
                tableModel.getRowCount());
        
        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();
        buttonPanel.add(addDataButton);
        addDataButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                addDataActionPerformed();
            }
        });
        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

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

            @Override
            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
            model.addRow(row);
        }
    }

    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() {

            @Override
            public void run() {
                JFrame frame = new JFrame("DefaultTableModelDemo");
                frame.getContentPane().add(new DefaultTableModelDemo().getComponent());
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
}

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();
        
        defaultTableModel.addRow(val);


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