Question

Can someone help me with this? It was working until I changed something trying to optimaze it... damn!

This is my table model:

class MyTableModel extends DefaultTableModel {

        private String[] columnNames = {"Last Name","First Name","Next Visit"};     //column header labels
        Object[][] data = new Object[100][3];

        public void reloadJTable(List<Customer> list) {
            for(int i=0; i< list.size(); i++){
                data[i][0] = list.get(i).getLastName();
                data[i][1] = list.get(i).getFirstName();
                if (list.get(i).getNextVisit()==null) {
                    data[i][2] = "NOT SET";
                } else {
                    String date = displayDateFormat.format(list.get(i).getNextVisit().getTime());
                    data[i][2] = date;
                }
                model.addRow(data);
            }
        }

        public void clearJTable() {
            model.setRowCount(0);
        }

        public String getColumnName(int col) {
            return columnNames[col];
        }

        public Object getValueAt(int row, int col) {
            return data[row][col];
        }

        /*
         * JTable uses this method to determine the default renderer/
         * editor for each cell.  If we didn't implement this method,
         * then the last column would contain text ("true"/"false"),
         * rather than a check box.
         */
        public Class getColumnClass(int c) {
            return getValueAt(0, c).getClass();
        }

        /*
         * Don't need to implement this method unless your table's
         * editable.
        */ 
        public boolean isCellEditable(int row, int col) {
            //Note that the data/cell address is constant,
            //no matter where the cell appears onscreen.
            if (col < 2) {
                return false;
            } else {
                return true;
            }
        }
    }

and this is how I implement the JTable:

// these declarations are all private shared across the model
JTable customerTbl;
MyTableModel model;
List<Customer> customers = new ArrayList<Customer>(); 
SimpleDateFormat displayDateFormat = new SimpleDateFormat ("EEE dd-MM-yyyy 'at' hh:mm");

//JTable configuration
model = new MyTableModel();
customerTbl = new JTable(model);
model.reloadJTable(customers);
customerTbl.setAutoCreateRowSorter(true);  //enable row sorters                     

DefaultRowSorter sorter = ((DefaultRowSorter)customerTbl.getRowSorter());  //default sort by Last Name
ArrayList list = new ArrayList();
list.add( new RowSorter.SortKey(0, SortOrder.ASCENDING));
sorter.setSortKeys(list);  //EXCEPTION HERE
sorter.sort();

customerTbl.getColumnModel().getColumn(0).setPreferredWidth(100);   //set Last Name column preferred width
customerTbl.getColumnModel().getColumn(1).setPreferredWidth(80);    //set First Name column preferred width
customerTbl.getColumnModel().getColumn(2).setPreferredWidth(150);   //set Last Visit column preferred width   

I'm getting the following exception triggered on:

sorter.setSortKeys(list);

Exception in thread "main" java.lang.IllegalArgumentException: Invalid SortKey
    at javax.swing.DefaultRowSorter.setSortKeys(Unknown Source)
    at com.vetapp.customer.CustomersGUI.<init>(CustomersGUI.java:128)
    at com.vetapp.main.VetApp.main(VetApp.java:31)

I believe it has to do with the TableColumnModel which is not created correctly...

Was it helpful?

Solution

The main problem is that the column count is returning 0 from the TableModel's super class DefaultTableModel. You need to override this method

@Override
public int getColumnCount() {
   return columnNames.length;
}

Another side but potentially fatal issue is the fact that getColumnClass is returning the class of elements within the TableModel. This will throw a NullPointerException if the table is empty. Use a class literal instead such as String.class.

Maintaining a separate backing data array is unnecessary for DefaultTableModel. It has already has its own data vector. This approach is used when extending AbstractTableModel.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top