This question has been asked for a long time, and I do not know if you need this information ...
I understand your problem, the problem is that tree select listeners do not work as you might expect. You should detect a click event by registering a mouse listener. Something like that:
tree = new JTree();
tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
MouseListener ml = new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
int selRow = tree.getRowForLocation(e.getX(), e.getY());
currentRow = selRow;
TreePath selPath = tree.getPathForLocation(e.getX(), e.getY());
if (selRow != -1) {
DefaultMutableTreeNode node = new DefaultMutableTreeNode(selPath.getLastPathComponent());
if (node != null && node.isLeaf()) {
String stringId = '<sting-id-of-node-you-wish-to-scroll-to>';
TreePath tp = tree.getNextMatch(stringId, currentRow, Position.Bias.Forward);
if (tp == null) {
tp = tree.getNextMatch(stringId, currentRow, Position.Bias.Backward);
}
tree.setSelectionPath(tp);
tree.scrollPathToVisible(tp);
}
}
}
};
tree.addMouseListener(ml);
Hooray!
source
share