Java: How to insert rows(data) into JTable explicitly if rows are inserted by AbstractTableModel

35,310

Solution 1

Like this, you can implement other method like deleteRow(int rowIndex) and insertRowToIndex(int rowIndex, List rowData).

Rememer after you change the data, you have to fire the table event, like fireTableRowsInserted() etc

public static class MyTableModel extends AbstractTableModel
{
    private List<String> columnNames = new ArrayList();
    private List<List> data = new ArrayList();

    {
        columnNames.add("First Name");
        columnNames.add("Last Name");
        columnNames.add("Sport");
        columnNames.add("# of Years");
        columnNames.add("Vegetarian");
    }

    public void addRow(List rowData)
    {
        data.add(rowData);
        fireTableRowsInserted(data.size() - 1, data.size() - 1);
    }

    public int getColumnCount()
    {
        return columnNames.size();
    }

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

    public String getColumnName(int col)
    {
        try
        {
            return columnNames.get(col);
        }
        catch(Exception e)
        {
            return null;
        }
    }

    public Object getValueAt(int row, int col)
    {
        return data.get(row).get(col);
    }

    public boolean isCellEditable(int row, int col)
    {
        return false;
    }

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

public static void main(String[] args)
{
    MyTableModel model = new MyTableModel();

    model.addRow(Arrays.asList("yi", "chen", "sleep", 35, true));

    JTable table = new JTable(model);
    table.setPreferredScrollableViewportSize(new Dimension(500, 70));
    table.setFillsViewportHeight(true);

    JScrollPane scrollPane = new JScrollPane(table);
    scrollPane.setBounds(5, 218, 884, 194);
    //now adding this to the frame where I want to show 
    JFrame frame = new JFrame();
    frame.add(scrollPane);
    frame.setVisible(true);
}

Solution 2

Just extending from DefaultTableModel instead of extending from AbstractTableModel should do the trick.

Solution 3

@Peter is right +1 use DefaultTableModel rather than AbstractTableModel, because your AbstractTableModel missed method addRow(); (and another methods too)

public void addRow(Object[][] data) {
    this.data.add(data);
    this.fireTableRowsInserted(data.size() - 1, data.size() - 1);
}

example about AbstractTableModel

Solution 4

AbstractTableModel will not do what you want to do, For this purpose you need to use DefaultTableModel,

When you use DefaultTableModel you can set isCellEditable or any other method in following ways,

    DefaultTableModel model = new DefaultTableModel(data, columnNames) {
        private static final long serialVersionUID = 1L;

        public boolean isCellEditable(int rowIndex, int columnIndex) {
            return false;
        }       
    };

in above code data will be String[ ][ ]. And by writing above code you are setting cell editable as false.

Other than this you can also load data in swingworker or afterwords like you might have done.

Share:
35,310
Vinit ...
Author by

Vinit ...

Development Engineer

Updated on June 25, 2020

Comments

  • Vinit ...
    Vinit ... almost 4 years

    In my application there is a JTable and I want to insert row after creating table.

    Following all codes are in the constructor of the frame.

    Code:

    private TableModel model = new AbstractTableModel(){
    String[] columnNames = {"First Name","Last Name","Sport","# of Years","Vegetarian"};
    private Object[][] data = {};
    
                public int getColumnCount() {
                    return columnNames.length;
                }
    
                public int getRowCount() {
                    return data.length;
                }
    
                public String getColumnName(int col) {
                    return columnNames[col];
                }
                public Class getColumnClass(int c) {
                    return getValueAt(0, c).getClass();
                }
    
                public Object getValueAt(int row, int col) {
                    return data[row][col];
                }
                public boolean isCellEditable(int row, int col) {
                    retuen false;
                }
                public Class getColumnClass(int c) {
                    return getValueAt(0, c).getClass();
                }
    };
    
    table = new JTable(model);
    table.setPreferredScrollableViewportSize(new Dimension(500, 70));
    table.setFillsViewportHeight(true);
    
    JScrollPane scrollPane = new JScrollPane(table);
    scrollPane.setBounds(5, 218, 884, 194);
    //now adding this to the frame where I want to show 
    frame.add(scrollPane);
    

    now I want to insert rows or data into the table. How this is possible. Previously I used DefaultTableModel but we cannot use isCellEditable and other method in DefaultTableModel so I change that with the code above. But in the above code I am not able to insert data (rows) explicitly, please help me.

  • Vinit ...
    Vinit ... about 12 years
    but if I use DefaultTableModel then I an not able to use isCellEditable and other method in my table thats why I change DefaultTableModel to AbstractTableModel.
  • Vinit ...
    Vinit ... about 12 years
    I not want to edit the data. I want to insert the data or rows.
  • Vinit ...
    Vinit ... about 12 years
    ok ...if I want to add row form other method of the application then what is the code?
  • kleopatra
    kleopatra about 12 years
    +1 for extending AbstractTableModel for custom api. But: doing so in an anonymous class doesn't make much sense .. that's probably what @VinitVikash is stumbling about :-)
  • kleopatra
    kleopatra about 12 years
    you need to use DefaultTableModel not necessarily: extending AbstractXX and adding api will do the trick as well :-)