Basically you should Iterate
inside JTree
, but TreeSelectionListener
can return an interesting value, like
import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTree; import javax.swing.SwingUtilities; import javax.swing.event.TreeSelectionEvent; import javax.swing.event.TreeSelectionListener; public class TreeSelectionRow { public TreeSelectionRow() { JTree tree = new JTree(); TreeSelectionListener treeSelectionListener = new TreeSelectionListener() { @Override public void valueChanged(TreeSelectionEvent treeSelectionEvent) { JTree treeSource = (JTree) treeSelectionEvent.getSource(); System.out.println("Min: " + treeSource.getMinSelectionRow()); System.out.println("Max: " + treeSource.getMaxSelectionRow()); System.out.println("Lead: " + treeSource.getLeadSelectionRow()); System.out.println("Row: " + treeSource.getSelectionRows()[0]); } }; tree.addTreeSelectionListener(treeSelectionListener); String title = "JTree Sample"; JFrame frame = new JFrame(title); frame.add(new JScrollPane(tree)); frame.setSize(300, 150); frame.setVisible(true); } public static void main(String args[]) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { TreeSelectionRow treeSelectionRow = new TreeSelectionRow(); } }); } }
source share