Preserve JTable selection across TableModel change

前端 未结 7 1614
北荒
北荒 2021-02-07 11:52

We\'re seeing JTable selection get cleared when we do a fireTableDataChanged() or fireTableRowsUpdated() from the TableModel.

相关标签:
7条回答
  • 2021-02-07 12:12

    This is default behavior. If you call fireTableDataChanged() the entire table is rebuild from scratch as you set entirely new model. In this case the selection is, naturally, lost. If you call fireTableRowsUpdated() the selection is also cleared in general cases. The only way is to remember selection and then set this. Unfortunately there is no guarantee that the selection will be still valid. Be careful if restoring selection.

    0 讨论(0)
  • 2021-02-07 12:19

    I was facing same issue and when tried to search the reason I got this question but it seems a bug in Java SDK. http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4276786

    WORK AROUND

    A temporary work-around is available. It should be removed once this bug is fixed as it's suitability has NOT been tested against fixed releases.

    Use this subclass of JTable.

    Note: This is for the MetalLookAndFeel. If using other look and feels, the inner FixedTableUI subclass will have to extend the TableUI subclass for that look and feel.

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.table.*;
    import javax.swing.event.*;
    import javax.swing.plaf.basic.*;
    
    public class FixedTable extends JTable {
    
      private boolean isControlDownInDrag;
    
      public FixedTable(TableModel model) {
          super(model);
          setUI(new FixedTableUI());
      }
    
      private class FixedTableUI extends BasicTableUI {
          private MouseInputHandler handler = new MouseInputHandler() {
              public void mouseDragged(MouseEvent e) {
                  if (e.isControlDown()) {
                      isControlDownInDrag = true;
                  }
                  super.mouseDragged(e);
              }
    
              public void mousePressed(MouseEvent e) {
                  isControlDownInDrag = false;
                  super.mousePressed(e);
              }
    
              public void mouseReleased(MouseEvent e) {
                  isControlDownInDrag = false;
                  super.mouseReleased(e);
              }
          };
    
          protected MouseInputListener createMouseInputListener() {
              return handler;
          }
      }
    
      public void changeSelection(int rowIndex, int columnIndex, boolean toggle, boolean extend) {
          if (isControlDownInDrag) {
              ListSelectionModel rsm = getSelectionModel();
              ListSelectionModel csm = getColumnModel().getSelectionModel();
    
              int anchorRow = rsm.getAnchorSelectionIndex();
              int anchorCol = csm.getAnchorSelectionIndex();
    
              boolean anchorSelected = isCellSelected(anchorRow, anchorCol);
    
              if (anchorSelected) {
                  rsm.addSelectionInterval(anchorRow, rowIndex);
                  csm.addSelectionInterval(anchorCol, columnIndex);
              } else {
                  rsm.removeSelectionInterval(anchorRow, rowIndex);
                  csm.removeSelectionInterval(anchorCol, columnIndex);
              }
    
              if (getAutoscrolls()) {
                  Rectangle cellRect = getCellRect(rowIndex, columnIndex, false);
                  if (cellRect != null) {
                      scrollRectToVisible(cellRect);
                  }
              }
          } else {
              super.changeSelection(rowIndex, columnIndex, toggle, extend);
          }
      }
    }
    

    Note Curtsey to http://bugs.sun.com

    0 讨论(0)
  • 2021-02-07 12:25

    for reference, as @Swapnonil Mukherjee stated, this did the trick with a table with selectable rows:

    // preserve selection calling fireTableDataChanged()
    final int[] sel = table.getSelectedRows();
    
    fireTableDataChanged();
    
    for (int i=0; i<sel.length; i++)
        table.getSelectionModel().addSelectionInterval(sel[i], sel[i]);
    
    0 讨论(0)
  • 2021-02-07 12:26

    I had the same issue in an application. In my case the model in the table was a list of objects, where the object properties where mapped to columns. In that case, when the list was modified, I retrieved the selected index and stored the object that was selected before updating the list. After the list is modified and before the table is updated, I would calculate the position of the selected object. If it was still present after the modification, then I would set the selection to the new index.

    Just setting the selected index in the table after the modification will not work, because the object may change position in the list.

    As a side note, I found that working with GlazedLists makes life much easier when dealing with tables.

    0 讨论(0)
  • 2021-02-07 12:31

    If I recall correctly, saving selection and re-applying it is what we have done too...

    0 讨论(0)
  • 2021-02-07 12:34

    You need to preserve the selection and then re-apply it.

    First of all you will need to get a list of all the selected cells.

    Then when you re-load the JTable with the new data you need to programmatically re-apply those same selections.

    The other point I want to make is, if the number or rows or columns in your table are increasing or decreasing after each table model reload, then please don't bother preserving the selection.

    The user could have selected row 2 column 1 having a value say "Duck", before model updation. But after model updation that same data can now occur in row 4 column 1, and your original cell row 2 column 1 could have new data such as "Pig". Now if you forcibly set the selection to what it was before the model updation, this may not be what the user wanted.

    So programmatically selecting cells could be a double edged sword. Don't do it, if you are not sure.

    0 讨论(0)
提交回复
热议问题