当前位置: 代码迷 >> J2SE >> jtree事件监听有关问题
  详细解决方案

jtree事件监听有关问题

热度:204   发布时间:2016-04-24 12:38:32.0
jtree事件监听问题
看下下面父节点A,B,C( 后面是子节点)

A AA AB
B BA BB
C AA AB

我是在A中加了2个叶子AA,AB,然后把AA,BB,克隆一份加到C中,

我如何选中 A下面的叶子节点,C下面2个叶子也选中,如果A下面取消选中,C下面也取消选择呢?

我是在 JTree tree = new JTree();  
  tree.addTreeSelectionListener( new TreeSelectionListener()
  {
  public void valueChanged(TreeSelectionEvent e){
  //我的方法改怎么写呢?
  }
  };



------解决方案--------------------
应该可以了
Java code
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();    }}
------解决方案--------------------
这样可以了。楼上意思可能理解错了
Java code
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();  }}