private boolean moveWithinSelectedRange(JTable table,int dx,int dy,ListSelectionModel rsm,ListSelectionModel csm){
  int totalCount;
  int minX, maxX, minY, maxY;
  boolean rs=table.getRowSelectionAllowed();
  boolean cs=table.getColumnSelectionAllowed();
  if (rs && cs) {
    totalCount=table.getSelectedRowCount() * table.getSelectedColumnCount();
    minX=csm.getMinSelectionIndex();
    maxX=csm.getMaxSelectionIndex();
    minY=rsm.getMinSelectionIndex();
    maxY=rsm.getMaxSelectionIndex();
  }
 else   if (rs) {
    totalCount=table.getSelectedRowCount();
    minX=0;
    maxX=table.getColumnCount() - 1;
    minY=rsm.getMinSelectionIndex();
    maxY=rsm.getMaxSelectionIndex();
  }
 else   if (cs) {
    totalCount=table.getSelectedColumnCount();
    minX=csm.getMinSelectionIndex();
    maxX=csm.getMaxSelectionIndex();
    minY=0;
    maxY=table.getRowCount() - 1;
  }
 else {
    totalCount=0;
    minX=maxX=minY=maxY=0;
  }
  boolean stayInSelection;
  if (totalCount == 0 || (totalCount == 1 && table.isCellSelected(leadRow,leadColumn))) {
    stayInSelection=false;
    maxX=table.getColumnCount() - 1;
    maxY=table.getRowCount() - 1;
    minX=Math.min(0,maxX);
    minY=Math.min(0,maxY);
  }
 else {
    stayInSelection=true;
  }
  if (dy == 1 && leadColumn == -1) {
    leadColumn=minX;
    leadRow=-1;
  }
 else   if (dx == 1 && leadRow == -1) {
    leadRow=minY;
    leadColumn=-1;
  }
 else   if (dy == -1 && leadColumn == -1) {
    leadColumn=maxX;
    leadRow=maxY + 1;
  }
 else   if (dx == -1 && leadRow == -1) {
    leadRow=maxY;
    leadColumn=maxX + 1;
  }
  leadRow=Math.min(Math.max(leadRow,minY - 1),maxY + 1);
  leadColumn=Math.min(Math.max(leadColumn,minX - 1),maxX + 1);
  do {
    calcNextPos(dx,minX,maxX,dy,minY,maxY);
  }
 while (stayInSelection && !table.isCellSelected(leadRow,leadColumn));
  return stayInSelection;
}

private boolean moveWithinSelectedRange ( JTable table , int dx , int dy , ListSelectionModel rsm , ListSelectionModel csm ) { int totalCount ; int minX , maxX , minY , maxY ; boolean rs = table . getRowSelectionAllowed ( ) ; boolean cs = table . getColumnSelectionAllowed ( ) ; if ( rs && cs ) { totalCount = table . getSelectedRowCount ( ) * table . getSelectedColumnCount ( ) ; minX = csm . getMinSelectionIndex ( ) ; maxX = csm . getMaxSelectionIndex ( ) ; minY = rsm . getMinSelectionIndex ( ) ; maxY = rsm . getMaxSelectionIndex ( ) ; } else if ( rs ) { totalCount = table . getSelectedRowCount ( ) ; minX = 0 ; maxX = table . getColumnCount ( ) - 1 ; minY = rsm . getMinSelectionIndex ( ) ; maxY = rsm . getMaxSelectionIndex ( ) ; } else if ( cs ) { totalCount = table . getSelectedColumnCount ( ) ; minX = csm . getMinSelectionIndex ( ) ; maxX = csm . getMaxSelectionIndex ( ) ; minY = 0 ; maxY = table . getRowCount ( ) - 1 ; } else { totalCount = 0 ; minX = maxX = minY = maxY = 0 ; } boolean stayInSelection ; if ( totalCount == 0 || ( totalCount == 1 && table . isCellSelected ( leadRow , leadColumn ) ) ) { stayInSelection = false ; maxX = table . getColumnCount ( ) - 1 ; maxY = table . getRowCount ( ) - 1 ; minX = Math . min ( 0 , maxX ) ; minY = Math . min ( 0 , maxY ) ; } else { stayInSelection = true ; } if ( dy == 1 && leadColumn == - 1 ) { leadColumn = minX ; leadRow = - 1 ; } else if ( dx == 1 && leadRow == - 1 ) { leadRow = minY ; leadColumn = - 1 ; } else if ( dy == - 1 && leadColumn == - 1 ) { leadColumn = maxX ; leadRow = maxY + 1 ; } else if ( dx == - 1 && leadRow == - 1 ) { leadRow = maxY ; leadColumn = maxX + 1 ; } leadRow = Math . min ( Math . max ( leadRow , minY - 1 ) , maxY + 1 ) ; leadColumn = Math . min ( Math . max ( leadColumn , minX - 1 ) , maxX + 1 ) ; do { calcNextPos ( dx , minX , maxX , dy , minY , maxY ) ; } while ( stayInSelection && ! table . isCellSelected ( leadRow , leadColumn ) ) ; return stayInSelection ; }
called to move within the selected range of the given jtable . this method uses the table ' s notion of selection , which is important to allow the user to navigate between items visually selected on screen . this notion may or may not be the same as what could be determined by directly querying the selection models . it depends on certain table properties ( such as whether or not row or column selection is allowed ) . when performing modifications , it is recommended that caution be taken in order to preserve the intent of this method , especially when deciding whether to query the selection models or interact with jtable directly .
