下面列出了javax.swing.ListSelectionModel#setAnchorSelectionIndex ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Set the lead and anchor without affecting selection.
*/
public static void setLeadAnchorWithoutSelection(ListSelectionModel model,
int lead, int anchor) {
if (anchor == -1) {
anchor = lead;
}
if (lead == -1) {
model.setAnchorSelectionIndex(-1);
model.setLeadSelectionIndex(-1);
} else {
if (model.isSelectedIndex(lead)) {
model.addSelectionInterval(lead, lead);
} else {
model.removeSelectionInterval(lead, lead);
}
model.setAnchorSelectionIndex(anchor);
}
}
/**
* Invokes <code>getNextIndex</code> to determine the next index
* to select. If the index is valid (not -1 and < size of the model),
* this will either: move the selection to the new index if
* the selectionType == CHANGE_SELECTION, move the lead to the
* new index if selectionType == CHANGE_LEAD, otherwise the
* selection is extend from the anchor to the new index and the
* lead is set to the new index.
*/
public void actionPerformed(ActionEvent e) {
int index = getNextIndex();
if (index >= 0 && index < list.getModel().getSize()) {
ListSelectionModel lsm = list.getSelectionModel();
if (selectionType == EXTEND_SELECTION) {
int anchor = lsm.getAnchorSelectionIndex();
if (anchor == -1) {
anchor = index;
}
list.setSelectionInterval(anchor, index);
lsm.setAnchorSelectionIndex(anchor);
lsm.setLeadSelectionIndex(index);
}
else if (selectionType == CHANGE_SELECTION) {
list.setSelectedIndex(index);
}
else {
lsm.setLeadSelectionIndex(index);
}
ensureIndexIsVisible(index);
}
}
public void setTo(ListSelectionModel sm) {
sm.clearSelection();
sm.setSelectionMode(selectionMode);
for (int[] itv : intervals) {
sm.addSelectionInterval(itv[0], itv[1]);
}
sm.setAnchorSelectionIndex(anchor);
sm.setLeadSelectionIndex(lead);
}