使用JTree在Java中获取每个子类和兄弟类的名称

时间:2016-03-06 11:56:44

标签: java swing jtree

我创建了JavaTree。现在我想要获取每个类的所有子类和兄弟类的名称。我可以使用selectedNode.getParent().toString()参加超级课程。但是下面的子类代码给出了java.util.Vector$1@3ae19358和兄弟类的结果,我找不到方法。有没有办法获取每个子类和兄弟类的名称?

    import java.awt.BorderLayout;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JScrollPane;
    import javax.swing.JTree;
    import javax.swing.SwingUtilities;
    import javax.swing.event.TreeSelectionEvent;
    import javax.swing.event.TreeSelectionListener;
    import javax.swing.tree.DefaultMutableTreeNode;

    public class TreeExample extends JFrame
    {

        /****************** Developing a Simple JTree************************/
      private JTree tree;
      private JLabel selectedLabel;  //for Event Handlers

      public TreeExample()
      {
          //create the root node
          DefaultMutableTreeNode root = new DefaultMutableTreeNode("AA");
          //create the child nodes
          DefaultMutableTreeNode bb = new DefaultMutableTreeNode("BB");
          DefaultMutableTreeNode cc = new DefaultMutableTreeNode("CC");

          //add the child nodes to the root node
          root.add(bb);
          root.add(cc);

          //create the tree by passing in the root node
          tree = new JTree(root);
          add(tree);

          this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          this.setTitle("JTree Example");       
          this.pack();
          this.setVisible(true);


    /****************** Adding More Children************************/    
          videoInfoNode.add(new DefaultMutableTreeNode("DD"));

          foodInfoNode.add(new DefaultMutableTreeNode("EE"));
          foodInfoNode.add(new DefaultMutableTreeNode("FF"));
          foodInfoNode.add(new DefaultMutableTreeNode("GG"));

    //To get the selected node information       
          tree.getSelectionModel().addTreeSelectionListener(new TreeSelectionListener() {
              @Override
              public void valueChanged(TreeSelectionEvent e) {
                  DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();

                  //sub class
                  if(selectedNode.getAllowsChildren()){----??????????
                  selectedLabel.setText(selectedNode.children().toString());}-----?????????
                  else{
                    System.out.println("no children");
                  }

                  //sibling class
                System.out.println(selectedNode.getSibling().....);--------???????? 
               }
          });

    } //end

      //main
      public static void main(String[] args)
      {
          SwingUtilities.invokeLater(new Runnable() {
              @Override
              public void run() {
                  new TreeExample();
              }
          });  
      }  
    } 

1 个答案:

答案 0 :(得分:1)

  

我可以使用selectedNode.getParent()。toString()获取超级

我猜你的意思是$(window).focus(function() { //They are staying on the page console.log("You've chocen to stay") }); $(window).blur(function() { //They're leaving the tab alert("Sure you don't want to stay?"); });

  

如果(selectedNode.getAllowsChildren()){---- ____

也许您需要使用TreeNode#isLeaf()代替DefaultMutableTreeNode#getAllowsChildren()

TreeNode