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 !