Here is sample code I found. It works because the entire data structure is initialized in one statement.
Object[][] data =
{
{"Buy", "IBM", new Integer(1000), new Double(80.5), Boolean.TRUE},
{"Sell", "Dell", new Integer(2000), new Double(6.25), Boolean.FALSE},
{"Short Sell", "Apple", new Integer(3000), new Double(7.35), Boolean.TRUE},
{"Buy", "MicroSoft", new Integer(4000), new Double(27.50), Boolean.FALSE},
{"Short Sell", "Cisco", new Integer(5000), new Double(20), Boolean.TRUE}
};
DefaultTableModel model = new DefaultTableModel(data, columnNames)
{
public Class getColumnClass(int column)
{
return getValueAt(0, column).getClass();
}
};
I need to manage a data model with thousands of rows. I have tried five different approaches for loading values (One String, four Integers, and one Double make up a row). The only way I have been able to get the table to display at all is to treat everything as strings. This would require a comparator for the Double column as natural String ordering doesn't cut it.
Below I have shown my latest attempt. The crux of my problem is the ArrayStoreException that happens at the line indicated -->
class RankingsDataModel extends DefaultTableModel {
static final String s = "";
static final Integer i = 0;
static final Double d = 0.0;
static String[] columnNames = { "Key", "Net IMPs", "Count", "Max IMPs", "Min IMPs", "Avg IMPs" };
static int[] widths = new int[] { 16, 16, 16, 16, 16, 16 };
Object[][] rows = new Object[S.getCurrentArchive().partnerCensus.size() > 9 ? S.getCurrentArchive().partnerCensus.size() : 9][getColumnCount()];
public int getColumnCount() { return columnNames.length; }
public int getRowCount() { return S.getCurrentArchive().partnerCensus.size() > 9 ? S.getCurrentArchive().partnerCensus.size() : 9; }
public String getColumnName(int col) { return columnNames[col]; }
public Object getValueAt(int row, int col) { return rows[row][col]; }
public Class getColumnClass(int c) { return c == 0 ? s.getClass() : c == 5 ? d.getClass() : i.getClass(); }
public boolean isCellEditable(int row, int col) { return false; }
}
class Row {
Integer net;
Integer count;
Integer max;
Integer min;
Double avg;
}
private void loadRankingsTable) {
int r = 0;
model.rows = new String[S.getCurrentArchive().partnerCensus.size()][6];
for (String p : S.getCurrentArchive().partnerCensus) {
PartnerDealFilter pf = new PartnerDealFilter(p);
addModelRow(pf, p, r++);
}
}
private void addModelRow(DealFilter f, String key, int r) {
Archive a = S.getCurrentArchive().getFilteredArchive(f);
Integer max = -24; Integer min = 24;
Integer count = 0;
for (DealNode dn : a.getDeals()) {
Integer imps = dn.getIMPsVsPar();
if (imps != null) {
count++;
if (imps < min) min = imps;
if (imps > max) max = imps;
}
}
String[] strings = { key };
Integer[] integers = { count, a.getNetIMPs(), max, min };
Double[] doubles = { a.getAvgIMPs() != null ? a.getAvgIMPs() : -24.01 };
--> model.rows[r][0] = strings[0];
model.rows[r][1] = Integers[0]);
model.rows[r][2] = integers[1];
model.rows[r][3] = integers[2];
model.rows[r][4] = integers[3];
model.rows[r][5] = doubles[0];
}
Previous attempts involved simpler efforts. I cannot store my table data in the model. I know I am missing something simple. Please help.
I expected the data model to be loaded. I received a java.lang.ArrayStoreException in the AWT EventQueue thread.