4

デフォルトの NetBeans 編集でフォームを作成し、そのjTree上に を置きました。そこになんとなく「色」「スポーツ」「食」などの要素がまとまって生まれる。しかし、それは作成コードにはありません。それはどこから来て、どのように編集できますか...

私がjTree1.removeAll();すべてを行っても、まだそこにあります...そして、新しいアイテムをjTreeに追加するための私のコードは機能していません。

private void test(java.awt.event.MouseEvent evt) {
    //trying to remove all, but it does not remove anything
    jTree1.removeAll();

    //it does print it in debug meaning that this function is called
    System.out.println("qwe");

    //create the root node
    DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
    //create the child nodes
    DefaultMutableTreeNode child1 = new DefaultMutableTreeNode("Child 1");
    DefaultMutableTreeNode child2 = new DefaultMutableTreeNode("Child 2");

    //add the child nodes to the root node
    root.add(child1);
    root.add(child2);

    //now how do I add it to the tree?
    //???
}

jTree実行時にコンテンツを編集できる必要があります。

4

2 に答える 2

5

JTree次の問題は、このように作成します( docsJTree tree = new JTree()によると)サンプルノードがあります。ノード (root、child1、child2) を作成した後に次の行を追加すると、すべてが機能します。

DefaultTreeModel model =(DefaultTreeModel) jTree1.getModel();
model.setRoot(root);

jTree1.removeAll();また、他の目的に使用されていると呼ぶ必要はありません。( docs )

のチュートリアルを読むJTree

于 2013-11-14T13:17:07.867 に答える