• Post Reply Bookmark Topic Watch Topic
  • New Topic

Undoable ListSelectionEvent?  RSS feed

 
Josh Rehman
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greetings,
I have a JTable list of items that can be selected by the user and edited with some other controls in the panel. If the item has been edited and not saved and they attempt to move the selection, I present the user with a "Do you want to save" yes/no/cancel dialog.
The cancel case is difficult because the user navigation must be canceled. I would also settle for simply reselecting the previously selected row. However all attempts to do this within the ListSelectionListener have failed.
...Well the test code I wrote works. So it must be something else. I'll look into it.
private void initListener(){
final ListSelectionModel model = table.getSelectionModel();
model.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
model.addListSelectionListener(new ListSelectionListener(){
public void valueChanged(ListSelectionEvent e) {
// let's try to make row 3 inaccessible
int currentRow = model.getMinSelectionIndex();
int lastRow = (currentRow == e.getFirstIndex() ? e.getLastIndex() : e.getFirstIndex());
int maxRow = table.getRowCount() - 1; //not really necessary, just interesting

// and elided toString for the ListSelectionEvent
String out = "firstIndex=" + e.getFirstIndex();
out += " lastIndex=" + e.getLastIndex();
out += " isAdjusting=" + e.getValueIsAdjusting();
out += " selected= " + currentRow;
out += " lastSelected= " + lastRow;
out += " max= " + maxRow;
System.out.println(out);

if (!e.getValueIsAdjusting() && (currentRow == 3) && !isEmpty(lastRow)){
model.setSelectionInterval(lastRow, lastRow);
}
}
private boolean isEmpty(int row){
return row == -1;
}
});
}
 
Josh Rehman
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!