0

我正在创建这个应用程序,它在按钮单击事件上创建选项卡和 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;

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

4

3 回答 3

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");
    }
  });
 }
}
于 2014-03-27T06:56:08.997 回答
1

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

我认为是重新加载,但没有扩展。JTree 根节点有很多设置:handler 如何或不显示根节点等等。

我正在使用调试控制台列表,在其中我将模型数据转储为文本(也覆盖节点中的 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);

但是可能看不到,需要扩展,怎么做,那是另外一个问题

于 2012-09-03T12:49:20.277 回答
0

我制作了一个小示例应用程序来测试您的问题。我发现节点被正确添加,但没有扩展。此外,调用 tomodel.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);
      }
    });
  }
}
于 2012-09-03T13:13:47.927 回答