对象类型的JTree显示对象的一些属性,但在选中时返回整个对象



我有一个将问题添加到jtree的程序。Jtree上的每个问题都是question类型的,它有3个属性;name, id和questionText。我希望我的Jtree只显示问题名称,但是当节点被选中时,它应该在文本框中显示questionText。

问题是Jtree按照我想要的方式显示问题的名称,但是,当我选择树的一个节点时,我得到ClassCastException。这是因为我的treeSelectionListener方法中的getUserObject()。它获取getUserObject()返回的String,并尝试将其强制转换为Question类。我如何获得所选字符串的问题类属性,如questionText或id没有getUserObject()或有另一种方式来表示这棵树?

下面是我的代码:
public class Question 
{
 public String name;
 public String id;
 public String qText;  
 public Question(String name,String id, String text) 
 {
        this.name = name;
        this.id = id;
        this.qText = text;
 }
 public String getQuestion()
 { return qText; }
 public String getName()
 { return name;}
 public String getId()
 { return id;}
 public void setQuestionText(String text)
 { qText= text; }
 public void setId(String uid)
 { id = uid;}
 @Override
 public String toString() 
 { return   name; }
}

创建树的部分代码:

public Question rootQ = new Question("Questions", UUID.randomUUID().toString(), "What is your name?");
public DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode(rootQ.toString());
public JTree tree = new JTree(rootNode);
initialise(){
 //....other code to initilise
 panel.add(new JScrollPane(tree));
 tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
 tree.setShowsRootHandles(true);
 tree.addTreeSelectionListener(treeMenuClicked);
 tree.setRootVisible(true);
 tree.setVisible(true);
 panel.add(addChild());
}
public TreeSelectionListener treeMenuClicked = new TreeSelectionListener() 
{
public void valueChanged(TreeSelectionEvent e) 
{
    TreePath currentSelection = tree.getSelectionPath();
    if(currentSelection != null){
     DefaultMutableTreeNode currentNode =  (DefaultMutableTreeNode)currentSelection.getLastPathComponent();
     Object nodeInfo = currentNode.getUserObject();
     Question questionText = (Question)nodeInfo;
     txtQuestion.setText(questionText.getQuestion()); 
    }
}};
public JButton addChild()
{
    JButton btnAddChild = new JButton("Add Child");
    btnAddChild.addActionListener(new ActionListener() 
    {
        public void actionPerformed(ActionEvent arg0) 
        {   String userQuestion = txtQuestion.getText();
            TreePath currentSelection = tree.getSelectionPath();
            if (currentSelection != null) {
                Question createdQuestion = new Question(userQuestion,UUID.randomUUID().toString(),userQuestion);
                DefaultMutableTreeNode currentNode = (DefaultMutableTreeNode) currentSelection.getLastPathComponent();
                DefaultTreeModel model = ((DefaultTreeModel) tree.getModel());
                DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(createdQuestion.toString());
                currentNode.add(newNode);
                model.nodeStructureChanged(currentNode);
            }
        }});
    btnAddChild.setBounds(609, 374, 117, 47);
    return btnAddChild;
}

就像@Ajay在评论中暗示的那样,而不是

DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(createdQuestion.toString());

简单做

DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(createdQuestion);

当前设置用户对象为string的是你,而不是API。

另外,您可以通过tree.getLastSelectedPathComponent()获得所选节点,而不是总是获得选择路径然后调用TreePath.getLastPathComponent()

public void valueChanged(TreeSelectionEvent e) 
{
    DefaultMutableTreeNode currentNode = (DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
    if(currentNode != null){         
        Object nodeInfo = currentNode.getUserObject();
        Question questionText = (Question)nodeInfo;
        txtQuestion.setText(questionText.getQuestion()); 
    }
}};

最新更新