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...