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
To prevent selection I just subclassed DefaultTreeSelectionModel and overrode all the methods to check for objects that I didn't want to be selected (instances of "DisplayRepoOwner" in my example below). If the object was OK to be selected, I called the super method; otherwise I didn't. I set my JTree's selection model to an instance of that subclass.
public class MainTreeSelectionModel extends DefaultTreeSelectionModel {
public void addSelectionPath(TreePath path) {
if (path.getLastPathComponent() instanceof DisplayRepoOwner) {
return;
}
super.addSelectionPath(path);
}
public void addSelectionPaths(TreePath[] paths) {
for (TreePath tp : paths) {
if (tp.getLastPathComponent() instanceof DisplayRepoOwner) {
return;
}
}
super.addSelectionPaths(paths);
}
public void setSelectionPath(TreePath path) {
if (path.getLastPathComponent() instanceof DisplayRepoOwner) {
return;
}
super.setSelectionPath(path);
}
public void setSelectionPaths(TreePath[] paths) {
for (TreePath tp : paths) {
if (tp.getLastPathComponent() instanceof DisplayRepoOwner) {
return;
}
}
super.setSelectionPaths(paths);
}
}