Question

I have a sortable default table model with ListSelectionListener that listens for a doubleclick and then opens a details view of a particular column. This works fine, however, when I sort a column the listener no longer functions.

JTable foosTable = new JTable(model);
TableRowSorter<TableModel> fooSorter = new TableRowSorter<TableModel>(model);
foosTable.setRowSorter(fooSorter);
ListSelectionModel listMod = foosTable.getSelectionModel();
listMod.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
ListSelectionListener barListener = new ListSelectionListener(foosTable);
listMod.addListSelectionListener(barListener);
Was it helpful?

Solution

I have never used TableRowListener which seems to only have an itemChanged event. I usually stick to standard swing. Add a mouse listener to the table, grab the location of a click event and then handle it.

 jTable.addMouseListener(new java.awt.event.MouseAdapter() {
     public void mouseClicked(java.awt.event.MouseEvent evt) {
         Point p = new Point(evt.getX(), evt.getY());
         int col = jTable.columnAtPoint(p);
         int row = jTable.rowAtPoint(p);
         if (evt.getButton() == MouseEvent.BUTTON1)
         {
            if (evt.getClickCount() >= 2)
            {
               ...
               ...
            }
        });

Edit Setup a TableRowSorter:

  TableRowSorter<YourJTableModel> sorter =
        new TableRowSorter<YourJTableModel>(yourJTableModel);
   jTable.setRowSorter(sorter);

Because you are changing the row order, you will need to use convertColumnIndexToModel to get the correct model data for the view.

For more complex sorting/filtering needs you may want to try glazed lists.

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