Add row to JTable with AbstractTableModel

Gerret picture Gerret · Aug 19, 2013 · Viewed 11.1k times · Source

I need help at adding a empty row to my JTable. I use a AbstractTableModel for my JTable! I have here a small executable program for you so it is easier to help me.

Main Class - TestClass.java

package testproject;

import java.awt.event.ActionEvent;
import java.util.ArrayList;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JTable;

public class TestClass {

    String[] header = {"Name", "Quantity"};
    ArrayList<Object[]> data = new ArrayList<Object[]>();

    public TestClass() {
        data.add(new Object[]{"Apple", "234"});
        data.add(new Object[]{"Banana", "123"});
        data.add(new Object[]{"Cranberry", "346"});

        JFrame frame = new JFrame();
        JTable table = new JTable(new Model(data, header));
        JMenuBar menubar = new JMenuBar();
        JMenu editmenu = new JMenu("Edit");

        Action addrowaction = new AbstractAction("Add Row") {
            private static final long serialVersionUID = 1433684360133156145L;

            @Override
            public void actionPerformed(ActionEvent e) {
                data.add(new Object[9]);
            }
        };

        frame.add(table);

        frame.setJMenuBar(menubar);
        menubar.add(editmenu);
        editmenu.add(addrowaction);

        frame.setLocationByPlatform(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        frame.pack();
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new TestClass();
            }
        });
    }
}

Table Model - Model.java

package testproject;

import java.util.ArrayList;

import javax.swing.table.AbstractTableModel;

public class Model extends AbstractTableModel{
    private static final long serialVersionUID = 6889542282067432083L;

    private String[] header;
    private ArrayList<Object[]> data;

    public Model(ArrayList<Object[]> data, String[] header) {
        this.header = header;
        this.data = data;
    }

    @Override
    public int getColumnCount() {
        return header.length;
    }

    @Override
    public String getColumnName(int column) {
        return header[column];
    }

    @Override
    public int getRowCount() {
        return data.size();
    }

    @Override
    public Object getValueAt(int row, int column) {
        return data.get(row)[column];
    }

    @Override
    public void setValueAt(Object value, int row, int column) {
        data.get(row)[column] = value;
        fireTableCellUpdated(row, column);
    }
}

Basically I want to use a Action in the edit menu to add a empty row. Please do not answer with other links! I searched for it but don't understand it or it isnt suitable for my program!

EDIT: Updated the source as suggested by MadProgrammer! There is only one problem left. How I update the JTable so the new Row is displayed at the moment I have to resize the Window and the Table is updating!

  • Thank you for help

Answer

trashgod picture trashgod · Aug 19, 2013

As @MadProgrammer comments, arrays are conceptually easy to understand but hard to expand & contract. Instead, use a Collection. In this example, MyModel manages a List<Row> that is implemented as an ArrayList<Row>. Each Row is a Plain Old Java Object (POJO) having an attribute for each column. Try adding a method to insert new rows using the constructor as an example, then try adding a column to Row. The example uses a static nested class to avoid clutter.