在Java中以编程方式创建JTree添加节点

时间:2012-09-03 12:44:30

标签: java jtree

我正在创建这个应用程序,可以在按钮点击事件上创建标签和JTree。问题是当我尝试将ne节点添加到JTree时它不会刷新JTree(或者它没有向它添加节点......我真的不知道)。

这是创建标签和树的功能:

jTabbedPane1.add(st,jSplitpane10);
int count = jTabbedPane1.getTabCount();
jTabbedPane1.setSelectedIndex(count-1);

DefaultMutableTreeNode root = new DefaultMutableTreeNode("All Notebooks");
DefaultMutableTreeNode notebook1 = new DefaultMutableTreeNode("Notebook 1");
root.add(notebook1);

// Create tree
JTree tree = new JTree(root);
//Create Scroll Pane for the tree
JScrollPane sp = new JScrollPane(tree);

Global.trees.add(tree);

这是应该将新节点“Green”添加到所选选项卡中的树的代码:

int i = jTabbedPane1.getSelectedIndex();
DefaultTreeModel model = (DefaultTreeModel)Global.trees.get(i).getModel();

// Find node to which new node is to be added
int startRow = 0;
String prefix = "J";
TreePath path = Global.trees.get(i).getNextMatch(prefix, startRow, Position.Bias.Forward);
MutableTreeNode node = (MutableTreeNode)path.getLastPathComponent();

// Create new node
MutableTreeNode newNode = new DefaultMutableTreeNode("green");

// Insert new node as last child of node
model.insertNodeInto(newNode, node, node.getChildCount());
model.reload(newNode);

这里还有全球JTree列表的声明:

public class Global {
    public java.util.List<JTree> trees = new ArrayList<JTree>();
}Global Global;

为什么新节点没有在树中显示的任何想法???

3 个答案:

答案 0 :(得分:1)

JTree和JTable是最复杂的Swing组件。

我认为是重新加载,但没有扩展。 JTree根节点有许多设置:处理程序是否显示根节点等等。

我正在使用调试控制台列表,我将模型数据转储为文本(也覆盖节点中的toString()),我可以很容易地看到,有什么,但如果你只需要几个节点,对Netbeans的调试器来说已经足够了。

还尝试展开树中的所有行以使其可见。

您的代码很好:

// Create new node
MutableTreeNode newNode = new DefaultMutableTreeNode("green");

// Insert new node as last child of node
model.insertNodeInto(newNode, node, node.getChildCount());
model.reload(newNode);

但可能它不可见,你需要扩展,怎么做,这是另一个问题

答案 1 :(得分:1)

以下是一些以编程方式添加JTree的代码:

来源:http://sickprogrammersarea.blogspot.in/2014/03/add-jtree-programmatically-in-java-swing.html

import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.tree.*;
public class MyTree extends JApplet
{
  JTree t;
  JLabel l1;
  String lang[]={"C","C++","JAVA","PYTHON","AJAX","PHP"};
  public void init()
  {
    try
    {
     SwingUtilities.invokeAndWait(new Runnable() {
       public void run()
       {
          makeGUI();
       }
    });
    } catch(Exception e){
       System.out.println("Sorry some error occured "+e);
    }
}
private void makeGUI()
{
   DefaultMutableTreeNode top=new DefaultMutableTreeNode("Language");
   DefaultMutableTreeNode a=new DefaultMutableTreeNode("Programming");
   top.add(a);
   DefaultMutableTreeNode a1=new DefaultMutableTreeNode("C");
   a.add(a1);
   DefaultMutableTreeNode a2=new DefaultMutableTreeNode("Java");
   a.add(a2);
   DefaultMutableTreeNode b=new DefaultMutableTreeNode("Web Based");
   top.add(b);
   DefaultMutableTreeNode b1=new DefaultMutableTreeNode("PHP");
   b.add(b1);
   DefaultMutableTreeNode b2=new DefaultMutableTreeNode("JSP");
   b.add(b2);
   t=new JTree(top);
   JScrollPane scr=new JScrollPane(t);
   add(scr);
   setLayout(new  FlowLayout());
   l1=new JLabel("Choose a language");
   add(l1,BorderLayout.SOUTH);
   t.addTreeSelectionListener(new TreeSelectionListener () {
    public void valueChanged(TreeSelectionEvent ae) { 
      l1.setText(ae.getPath()+" is selected");
    }
  });
 }
}

答案 2 :(得分:0)

我制作了一个小示例应用程序来测试您的问题。我发现节点正在正确添加,但没有扩展。此外,似乎没有必要调用model.reload

我在树上添加了对expandPath的调用,以使显示器显示新节点。您可能需要仔细检查这不会过多地扩展树:

public class Example extends JFrame {

  private JTree tree;

  public Example() {
    DefaultMutableTreeNode root = new DefaultMutableTreeNode("All Notebooks");
    DefaultMutableTreeNode notebook1 = new DefaultMutableTreeNode("Notebook 1");
    root.add(notebook1);

    tree = new JTree(root);
    JScrollPane sp = new JScrollPane(tree);

    setLayout(new BorderLayout());
    add(sp, BorderLayout.CENTER);
    add(new JButton("Go") {
    {
      addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
          Example.this.addNode();
        }
      });
    }}, BorderLayout.SOUTH);

    setDefaultCloseOperation(EXIT_ON_CLOSE);
    pack();
  }


  private void addNode() {
    DefaultTreeModel model = (DefaultTreeModel) tree.getModel();

    // Find node to which new node is to be added
    int startRow = 0;
    String prefix = "N";
    TreePath path = tree.getNextMatch(prefix, startRow,
        Position.Bias.Forward);
    MutableTreeNode node = (MutableTreeNode) path.getLastPathComponent();

    // Create new node
    MutableTreeNode newNode = new DefaultMutableTreeNode("green");

    // Insert new node as last child of node
    model.insertNodeInto(newNode, node, node.getChildCount());
    tree.expandPath(path);
  }


  public static void main(String[] args) {    
    EventQueue.invokeLater(new Runnable() {      
      @Override
      public void run() {
        new Example().setVisible(true);
      }
    });
  }
}