これが私の解決策です。
JTree サブクラス:
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);
}
次に、クラスを使用してツリーで:
VetoableChangeListener vcl = new VetoableChangeListener() {
public void vetoableChange(PropertyChangeEvent evt) throws PropertyVetoException {
if ( evt.getPropertyName().equals(JTree.LEAD_SELECTION_PATH_PROPERTY) ) {
try {
<some code logic that has to be satisfied>
} catch (InvalidInputException e) {
throw new PropertyVetoException("", evt);
}
}
}
};
tree.addVetoableChangeListener(vcl);
メカニズムは可能な限り早い場所から開始されます。マウス操作がインターセプトされ、選択されるパスが VetoableChangeListeners にアドバタイズされます。具体的な VCL では、変化するプロパティが調べられ、リード選択の場合は拒否ロジックがチェックされます。拒否が必要な場合、VCL は PropertyVetoException をスローします。それ以外の場合、マウス イベントの処理は通常どおり行われ、選択が行われます。つまり、これにより、リード選択プロパティが制約付きプロパティになります。