I have a dialog where each entry in a JTree has its corresponding options in a different panel, which is updated when the selection changes. If options for one of the entrie
Here is my solution.
In a JTree subclass:
protected void processMouseEvent(MouseEvent e) {
TreePath selPath = getPathForLocation(e.getX(), e.getY());
try {
fireVetoableChange(LEAD_SELECTION_PATH_PROPERTY, getLeadSelectionPath(), selPath);
}
catch (PropertyVetoException ex) {
// OK, we do not want change to happen
return;
}
super.processMouseEvent(e);
}
Then in the tree using class:
VetoableChangeListener vcl = new VetoableChangeListener() {
public void vetoableChange(PropertyChangeEvent evt) throws PropertyVetoException {
if ( evt.getPropertyName().equals(JTree.LEAD_SELECTION_PATH_PROPERTY) ) {
try {
} catch (InvalidInputException e) {
throw new PropertyVetoException("", evt);
}
}
}
};
tree.addVetoableChangeListener(vcl);
The mechanism starts at the earliest possible place. Mouse action intercepted, the path to-be-selected is advertised to VetoableChangeListeners. In the concrete VCL the changing property is examined and if it is the lead selection, veto logic is checked. If vetoing is needed, the VCL throws PropertyVetoException, otherwise mouse event handling goes as usual and selection happens. In short, this makes lead selection property become a constrained property.