下面的示例演示如何使用该JTree.getSelectionPaths()方法来获取JTree组件的选定节点。此方法返回一个TreePath对象数组。如果要获取路径中的最后一个节点,我们可以调用TreePath.getLastPathComponent()。
在下面的代码中,当您按下按钮时,侦听器将获取选择路径并打印出所选节点的最后一个路径部分。
package org.nhooo.example.swing; import javax.swing.*; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.TreePath; import java.awt.*; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class JTreeGetSelectedNode extends JFrame { public JTreeGetSelectedNode() throws HeadlessException { initializeUI(); } private void initializeUI() { setSize(200, 400); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); DefaultMutableTreeNode root = new DefaultMutableTreeNode("Countries"); DefaultMutableTreeNode asia = new DefaultMutableTreeNode("Asia"); String[] countries = new String[] {"India", "Singapore", "Indonesia", "Vietnam"}; for (String country : countries) { DefaultMutableTreeNode node = new DefaultMutableTreeNode(country); asia.add(node); } DefaultMutableTreeNode northAmerica = new DefaultMutableTreeNode("North America"); countries = new String[] {"United States", "Canada"}; for (String country : countries) { DefaultMutableTreeNode node = new DefaultMutableTreeNode(country); northAmerica.add(node); } DefaultMutableTreeNode southAmerica = new DefaultMutableTreeNode("South America"); countries = new String[] {"Brazil", "Argetina", "Uruguay"}; for (String country : countries) { DefaultMutableTreeNode node = new DefaultMutableTreeNode(country); southAmerica.add(node); } DefaultMutableTreeNode europe = new DefaultMutableTreeNode("Europe"); countries = new String[] {"United Kingdom", "Germany", "Spain", "France", "Italy"}; for (String country : countries) { DefaultMutableTreeNode node = new DefaultMutableTreeNode(country); europe.add(node); } root.add(asia); root.add(northAmerica); root.add(southAmerica); root.add(europe); final JTree tree = new JTree(root); JScrollPane pane = new JScrollPane(tree); pane.setPreferredSize(new Dimension(200, 400)); JButton button = new JButton("Get Selected"); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { TreePath[] paths = tree.getSelectionPaths(); for (TreePath path : paths) { System.out.println("You've selected: " + path.getLastPathComponent()); } } }); getContentPane().setLayout(new BorderLayout()); getContentPane().add(pane, BorderLayout.CENTER); getContentPane().add(button, BorderLayout.SOUTH); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { new JTreeGetSelectedNode().setVisible(true); } }); } }