0

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.

DrSlamm
  • 1
  • 1
  • 1
    *"I need to manage a data model with thousands of rows"* -- wouldn't this amount of data be best handled by a database, with the GUI displaying "chunks" of data brought in from the database on an as-needed basis? – Hovercraft Full Of Eels Dec 09 '22 at 18:43
  • In effect, my Archive is the database, RAM-resident. Iterators, filters, sorters, all I need. Java is like a wet dream, only drier. – DrSlamm Dec 09 '22 at 19:45
  • That example uses hard coded data so you don't need to access a database or file to get the data and run the demo. You should NOT attempt to create a "dynamic" 2D Array of all your data. The internal storage of the DefaultTableModel uses a Vector of Vectors to hold all the data. A Vector is dynamic so it is much more flexible to use than a 2D Array. So there are two approaches to load the data. 1) Create the Vector of Vectors manually and then create the DefaultTableModel using this Vector. For a complete example of this approach check out: https://stackoverflow.com/a/55635012/131872. – camickr Dec 09 '22 at 21:04
  • 2) The other approach is to create a DefaultTableModel with only column headers and no row data. Then you can use the `addRow(...)` method of the DefaultTableModel to dynamically add one row of data at a time. See: https://stackoverflow.com/a/64248188/131872 for an example that uses the `addRow(...)` method. – camickr Dec 09 '22 at 21:10
  • Thx to all, after a bit of wrangling I got my table model and renderers working adequately if not perfectly. Having fun and this site is fantastic! – DrSlamm Dec 14 '22 at 19:25

1 Answers1

0

The initial code created an Object[][] array and you created a String[][] array, so, storing any non-String value will inevitably crash. You will need to create an Object[][] to support Integer and Double values as well.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175