Best way to stop a JTree selection change from happening?

前端 未结 7 2169
傲寒
傲寒 2021-01-06 05:07

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

相关标签:
7条回答
  • 2021-01-06 05:41

    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);
    }
    

    }

    0 讨论(0)
提交回复
热议问题