0

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();
    }
  • What does PopulateData do? Does it recreate the TableModel? I think when you recreate the model you need to reset the sorter. – camickr Jun 13 '23 at 14:21
  • If you're updating the table every 4 seconds, the user doesn't have time to do anything with the table. – Gilbert Le Blanc Jun 13 '23 at 16:06
  • @camickr the populateData function sets the values in the cells – Muthu0044 Jun 14 '23 at 09:17
  • @GilbertLeBlanc This table is to show some data of the devices we got, so the only thing user going to do is Start/Stop the device by right clicking the rows. It'll be helpful if they can select multiple row and do Start/Stop. Also need sorting of table by clicking the headers. – Muthu0044 Jun 14 '23 at 09:20
  • Fine. There's no need to update the table every 4 seconds. Oracle has a helpful tutorial, [Creating a GUI With Swing](https://docs.oracle.com/javase/tutorial/uiswing/index.html). Skip the Learning Swing with the NetBeans IDE section. Pay particular attention to the [How to Use Tables](https://docs.oracle.com/javase/tutorial/uiswing/components/table.html) section. – Gilbert Le Blanc Jun 14 '23 at 09:47
  • Post an [mre] demonstrating the problem. – camickr Jun 14 '23 at 13:37

0 Answers0