日期:2014-05-20 浏览次数:20728 次
import java.util.Enumeration; import javax.swing.BoxLayout; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTree; import javax.swing.event.TreeSelectionEvent; import javax.swing.event.TreeSelectionListener; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.DefaultTreeModel; import javax.swing.tree.TreeNode; import javax.swing.tree.TreePath; class TreeFrame implements TreeSelectionListener { private JTree example = null; public TreeFrame() { DefaultMutableTreeNode root = new DefaultMutableTreeNode(" root ", true); DefaultMutableTreeNode a = new DefaultMutableTreeNode(" A "); root.add(a); a.add(new DefaultMutableTreeNode(" AA ")); a.add(new DefaultMutableTreeNode(" AB ")); DefaultMutableTreeNode b = new DefaultMutableTreeNode(" B "); root.add(b); b.add(new DefaultMutableTreeNode(" BA ")); b.add(new DefaultMutableTreeNode(" BB ")); DefaultMutableTreeNode c = new DefaultMutableTreeNode(" C "); root.add(c); c.add(new DefaultMutableTreeNode(" AA ")); c.add(new DefaultMutableTreeNode(" AB ")); example = new JTree(root); example.addTreeSelectionListener(this); JPanel panel = new JPanel(); panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS)); panel.add(new JScrollPane(example)); JFrame frame = new JFrame(" JTreeDemo "); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setContentPane(panel); frame.setSize(250, 400); frame.setVisible(true); } public void valueChanged(TreeSelectionEvent e) { example.expandPath(e.getPath()); DefaultMutableTreeNode dm = (DefaultMutableTreeNode) e.getPath() .getLastPathComponent(); for (Enumeration em = dm.children(); em.hasMoreElements();) { TreeNode n = (TreeNode) em.nextElement(); TreePath visiblePath = new TreePath(((DefaultTreeModel) example .getModel()).getPathToRoot(n)); example.addSelectionPath(visiblePath); } } } public class TreeTest { public static void main(String[] args) { new TreeFrame(); } }
------解决方案--------------------
这样可以了。楼上意思可能理解错了
import java.util.ArrayList; import java.util.Enumeration; import java.util.List; import javax.swing.BoxLayout; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTree; import javax.swing.event.TreeSelectionEvent; import javax.swing.event.TreeSelectionListener; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.DefaultTreeModel; import javax.swing.tree.TreeNode; import javax.swing.tree.TreePath; class TreeFrame implements TreeSelectionListener { private JTree example = null; private List<TreeNode> allLeaf = new ArrayList<TreeNode>(); public TreeFrame() { DefaultMutableTreeNode root = new DefaultMutableTreeNode(" root ", true); DefaultMutableTreeNode a = new DefaultMutableTreeNode(" A "); root.add(a); a.add(new DefaultMutableTreeNode(" AA ")); a.add(new DefaultMutableTreeNode(" AB ")); DefaultMutableTreeNode b = new DefaultMutableTreeNode(" B "); root.add(b); b.add(new DefaultMutableTreeNode(" BA ")); b.add(new DefaultMutableTreeNode(" BB ")); DefaultMutableTreeNode c = new DefaultMutableTreeNode(" C "); root.add(c); c.add(new DefaultMutableTreeNode(" AA ")); c.add(new DefaultMutableTreeNode(" AB ")); example = new JTree(root); example.addTreeSelectionListener(this); JPanel panel = new JPanel(); panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS)); panel.add(new JScrollPane(example)); JFrame frame = new JFrame(" JTreeDemo "); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setContentPane(panel); frame.setSize(250, 400); frame.setVisible(true); visitAllNodes(example); } public void valueChanged(TreeSelectionEvent e) { example.expandPath(e.getPath()); DefaultMutableTreeNode dm = (DefaultMutableTreeNode) e.getPath().getLastPathComponent(); DefaultMutableTreeNode root = (DefaultMutableTreeNode) example.getModel().getRoot(); for (TreeNode leaf : allLeaf) { if (leaf.toString().equals(dm.toString())) { TreePath visiblePath = new TreePath(((DefaultTreeModel) example.getModel()) .getPathToRoot(leaf)); example.addSelectionPath(visiblePath); } } } public void visitAllNodes(JTree tree) { DefaultMutableTreeNode root = (DefaultMutableTreeNode) tree.getModel().getRoot(); Enumeration breadthFirst = root.breadthFirstEnumeration(); while (breadthFirst.hasMoreElements()) { TreeNode node = (TreeNode) breadthFirst.nextElement(); if (node.isLeaf()) { allLeaf.add(node); } } } } public class TreeTest { public static void main(String[] args) { new TreeFrame(); } }