You have two choices, the first of which is to flip your own JTree subclass, which looks something like this:
public class MyTree extends JTree { public MyTree() { final MyTreeModel m = new MyTreeModel() super.setTreeModel(m); super.setSelectionModel(m.getSelectionModel()); } private class MyTreeModel implements TreeModel() { private final DefaultTreeSelectionModel sm; public MyTreeModel() { this.sm = new DefaultTreeSelectionModel(); } public TreeSelectionModel getSelectionModel() { return this.sm; } } }
Thus, you always have a selection model if necessary, and you can change it in your move method.
On the other hand, similar functionality already exists in drag and drop support and a template for supporting DnD in a significant but manageable way (plus, you get, well, DnD support in your tree). The problem is that I do not know how to programmatically fire the DnD event.
source share