Preserve JTable selection across TableModel change

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

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

7条回答
  •  梦毁少年i
    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

提交回复
热议问题