Are there any event that is fired when cell is about to be selected? There is ListSelectionListener, but it has event that is fired only after selection has happened. I need
whatever the goal is that you want to achieve: thinking "mouseEvent" is not enough, selection might change for other reasons (f.i. keyboard input, programmatic trigger, ..). Reverting an unwated change in a listener is not an option: as you already noted that would require to keep a duplicate of the selection and might confuse other listeners.
The only way (that I see, could be others, of course ;-) is not to let it happen in the first place: implement a List SelectionModel which doesn't change the selection if certain conditions are met. My favourite (biased me :-) is a VetoableListSelectionModel It's a subclass of DefaultListSelectionModel which in SingleSelectionMode waits for vetoes from interested parties before actually changing.
Here's a (raw) code snippet using it:
VetoableListSelectionModel vetoableSelection = new VetoableListSelectionModel();
VetoableChangeListener navigationController = new VetoableChangeListener() {
public void vetoableChange(PropertyChangeEvent evt)
throws PropertyVetoException {
// custom method that implements your condition
if (!canSelect((int) evt.getOldValue(), (int) evt.getNewValue()))
throw new PropertyVetoException("uncommitted changes",
evt);
}
};
vetoableSelection.addVetoableChangeListener(navigationController);
myTable.setSelectionModel(vetoableSelection);