How To Get Selected Node Index From JTree Using Java NetBeans
In this Java Tutorial we will see How To Get The Selected JTree Node Index Using DefaultMutableTreeNode + TreeSelectionModel And Display The Value Into A JTextFields On JTree Mouse Clicked Event In Java programming language & NetBeans IDE .
Project Source Code:
private void jTree1MouseClicked(java.awt.event.MouseEvent evt) {
DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) jTree1.getSelectionPath().getLastPathComponent();
if(selectedNode != jTree1.getModel().getRoot())
{
int selectedNodeIndex = selectedNode.getParent().getIndex(selectedNode);
jTextField1.setText(Integer.toString(selectedNodeIndex));
}
TreeSelectionModel model = jTree1.getSelectionModel();
if(model.getSelectionCount() > 0)
jTextField1.setText(Integer.toString(model.getSelectionRows()[0]));
}