Why does the following code result in a java.lang.ArrayIndexOutOfBoundsException: 0 >= 0 ?
JTable oTable = new JTable();
for (int row = 0; row < table.getRowCount(); row++) {
for (int column = 0; column < table.getColumnCount(); column++) {
oTable.setValueAt(table.getValueAt(row, column), row, column);
}
}
Table has been sorted using SortKeys. After SortKeys works, the view in the GUI is updated but the underlying data (model) has not been changed. I need to either change the underlying model or create a new table with the data from the view.
DefaultTableModel model = (DefaultTableModel) table.getModel();
TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(model);
table.setRowSorter(sorter);
List<RowSorter.SortKey> sortKeys = new ArrayList<RowSorter.SortKey>();
sortKeys.add(new RowSorter.SortKey(model.findColumn("col title"), SortOrder.DESCENDING));
sortKeys.add(new RowSorter.SortKey(model.findColumn("col title 2"), SortOrder.DESCENDING));
sortKeys.add(new RowSorter.SortKey(model.findColumn("another col title"), SortOrder.DESCENDING));
sorter.setSortKeys(sortKeys); //this should trigger a .sort()
I don’t know why the model does not update but it doesn’t.
You are using two JTable variables,
oTable, andtable, and theoTableJTable has 0 columns and 0 rows, but the JTable referred by table probably doesn’t. This will result in Java looking for rows and columns that don’t exist. Don’t mix and match JTable variables this way.If you want the table and the oTable JTables to hold the same values, simply pass the model over: