How to keep selections and make sorting to work on AbstractTableModel in java when the cells are updating frequently?

30 views Asked by At

I'm using AbstractTableModel, enabled sorting and multiple row selection. the cells are being updated for every 4 secs, when I enable this populating services, sorting is not working and the selected rows gets unselected.

This is the TableModel I'm using.

public class TableModel extends AbstractTableModel {
    private static final long serialVersionUID = 1L;
    private final String[] columnNames = {null, null, null, null, null, null, null, null, null, null}; //length = 10
    public Object[][] RowData = new Object[TableProperties.row][TableProperties.column];

    public TableModel() {

    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        return columnIndex == 0 ? Date.class : Integer.class;
    }

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

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

    public boolean isCellEditable(int row, int column) {
        return false;// Added for disable the table cell Edit.
    }

    public int getRowCount() {
        return RowData.length;
    }

    public void setValueAt(Object value, int row, int col) {
        RowData[row][col] = value;
        fireTableCellUpdated(row, col);
    }
    public void removeRow(int row) {
        // remove a row from your internal data structure
        fireTableRowsDeleted(row, row);
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        return RowData[rowIndex][columnIndex];
    }


}

**This is how I enabled row selection and column sorting. **

mod = new TableModel();
        mainTable = new TableView(mod);
        //tr = new ColorRenderer();
        mainTable.setCellSelectionEnabled(false);
        mainTable.setRowSelectionAllowed(true);
        mainTable.setRowHeight(cellHeight);
        mainTable.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        //mainTable.setAutoCreateRowSorter(true);  //enables sorting for all columns

        //Enables sorting for custom columns
        TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(mod) {
            @Override
            public boolean isSortable(int column) {
                // Define which columns are sortable
                List<Integer> sortableColumns = new ArrayList<>();
                sortableColumns.add(0); // Column 0 (Dev)
                sortableColumns.add(1); // Column 1 (Type)
                sortableColumns.add(3); // Column 3 (Active)
                sortableColumns.add(5); // Column 5 (Status)
                sortableColumns.add(9); // Column 9 (Location)

                return sortableColumns.contains(column);
            }
        };
        mainTable.setRowSorter(sorter);

This is how I'm running service codes.

  public void serviceMethod() {
        scheduler = Executors.newScheduledThreadPool(1);
        scheduler.scheduleAtFixedRate(new PopulateData(), 0, 4, TimeUnit.SECONDS);
        //PopulateData sd = new PopulateData();
        //sd.run();
    }
0

There are 0 answers