Question

I am trying to create an empty table with 5 columns and adding rows to it. I have created a separate tablemodel class that extends AbstractTableModel.

The class is :

public class MyTableModel extends AbstractTableModel 
 {
    private String[] columnNames = {"Name",
                                    "Size",
                                    "Directory",
                                    "Last Modified Time",
                                    "Readable"};

    Object[][] data=new Object[][]{

    };

    public int getColumnCount() 
    {
        return columnNames.length;
    }
    public String getColumnName(int col) 
    {
        return columnNames[col];
    }
    public Class getColumnClass(int c) 
    {
        return getValueAt(0, c).getClass();
    }
    public boolean isCellEditable(int row, int col) 
    {
            if (col < 1) 
        {
            return false;
        } 
        else 
        {
            return false;
        }
    }

    public void setValueAt(Object value, int row, int col) 
    {
        data[row][col]=value;

    }
    public void updateJarTable(Object[] row)

    {
        setValueAt(row[0],0,0);
        fireTableDataChanged();
        setValueAt(row[1],0,1);
        fireTableDataChanged();
        setValueAt(row[2],0,2);
        fireTableDataChanged();
        setValueAt(row[3],0,3);
        fireTableDataChanged();
        setValueAt(row[4],0,4);
        fireTableDataChanged();
    }
    @Override
    public int getRowCount() {
        // TODO Auto-generated method stub
        return data.length;
    }
    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        // TODO Auto-generated method stub
        return data[rowIndex][columnIndex];
    }
}

After this i am creating a table and applying this tablemodel to it as :

table=new JTable(new MyTableModel());   

when i click a button, a new row should be appended.

submit.addActionListener(new ActionListener () {
public void actionPerformed(ActionEvent e)
{
Object[] row={"col1","col2","col3","col4","col5"};
model1.updateJarTable(row);
table.revalidate();
}
}); 

Where model1 is :

private MyTableModel model1=new MyTableModel();

But when i run it am getting ArrayIndexOutofBoundsException at the methods : updateJarTable and setValueAt

I don't know where i have gone wrong. please help me to find it. thanks !

Was it helpful?

Solution 2

Take a moment and have a look at your code...

First you do...

table=new JTable(new MyTableModel());   

Then you do...

model1.updateJarTable(row);

How is model1 related to the model you assigned to the table?

Try using...

table=new JTable(model1);   

instead, just make sure that model1 is initialised BEFORE you apply it to the table and that you don't reassign it later (doing something like model1 = MyTableModel() again...

The ArrayIndexOutofBoundsException is down to the fact that you have an empty array

Object[][] data=new Object[][]{};

0 rows, 0 columns.

You either need to recreate the array to accomidate the new data, which isn't all that easy or use a more dynamic data structure, like java.util.List or simple use a DefaultListModel which does this.

Updated with List based model

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.swing.table.AbstractTableModel;

public class MyTableModel extends AbstractTableModel {

    private String[] columnNames = {"Name",
        "Size",
        "Directory",
        "Last Modified Time",
        "Readable"};

    List<List> rows;

    public MyTableModel() {
        rows = new ArrayList<>(25);
    }

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

    @Override
    public String getColumnName(int col) {
        return columnNames[col];
    }

    @Override
    public Class getColumnClass(int c) {
        // Don't do this, what happens if the cell value is null??
        return getValueAt(0, c).getClass();
    }

    @Override
    public boolean isCellEditable(int row, int col) {
        if (col < 1) {
            return false;
        } else {
            return false;
        }
    }

    @Override
    public void setValueAt(Object value, int row, int col) {
        List columns = rows.get(row);
        columns.set(col, value);
        fireTableCellUpdated(row, col);
    }

    public void updateJarTable(Object[] row) {
        List columns = new ArrayList(row.length);
        columns.addAll(Arrays.asList(row));
        rows.add(columns);
        fireTableRowsInserted(rows.size() - 1, rows.size() - 1);
    }

    @Override
    public int getRowCount() {
        // TODO Auto-generated method stub
        return rows.size();
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        List columns = rows.get(rowIndex);
        return columns.get(rowIndex);
    }
}

Or you could just do...

String[] columnNames = {"Name",
    "Size",
    "Directory",
    "Last Modified Time",
    "Readable"};
DefaultTableModel model = new DefaultTableModel(columnNames, 0);
//...
Object[] row={"col1","col2","col3","col4","col5"};
model.addRow(row);

And save yourself the trouble...

OTHER TIPS

Seems your problem in next lines:

Object[][] data=new Object[][]{

};

You have array Object[0][] because you can't set value to that and catch ArrayIndexOutOfBoundsException.

1) You can specify dimension of your array.

2) Or store data in List for example.

3) Programatically extend array, if that need, when you set values.

Also try DefaultTableModel.

I have already posted an answer in the same context.

Please find it here how to add an array to a Jtable when button is clicked java.

Use DefaultTableModel and simple call addRow() method on it to add a new row.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top