Java 对象类型的JTree显示对象的某些属性,但选中时返回整个对象
我有一个向jtree添加问题的程序。Jtree上的每个问题都是类型问题,具有3个属性;姓名、id和文本。我希望我的Jtree只显示问题名称,但当选择节点时,它应该在文本框中显示问题文本 问题是Jtree按我所希望的方式显示问题的名称,但是,当我选择树的一个节点时,我得到一个ClassCastException。这是因为我的treeSelectionListener方法中的getUserObject。它获取getUserObject返回的字符串,并尝试将其强制转换为问题类。如何在没有getUserObject的情况下获取所选字符串的问题类属性,如questionText或id,或者是否有其他方法来表示此树 这是我的密码:Java 对象类型的JTree显示对象的某些属性,但选中时返回整个对象,java,swing,jtree,Java,Swing,Jtree,我有一个向jtree添加问题的程序。Jtree上的每个问题都是类型问题,具有3个属性;姓名、id和文本。我希望我的Jtree只显示问题名称,但当选择节点时,它应该在文本框中显示问题文本 问题是Jtree按我所希望的方式显示问题的名称,但是,当我选择树的一个节点时,我得到一个ClassCastException。这是因为我的treeSelectionListener方法中的getUserObject。它获取getUserObject返回的字符串,并尝试将其强制转换为问题类。如何在没有getUser
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=新的DefaultMutableTreeNodecreatedQuestion.toString; 干脆 DefaultMutableTreeNode newNode=新的DefaultMutableTreeNodeDecreatedQuestion; 您当前正在将用户对象设置为字符串,而不是API 另外,您可以通过tree.getLastSelectedPathComponent获取所选节点,而不是总是获取选择路径,然后调用TreePath.getLastPathComponent public void valuechangedReelectionEvent e { DefaultMutableTreeNode currentNode=DefaultMutableTreeNodetree.getLastSelectedPathComponent; ifcurrentNode!=null{ Object nodeInfo=currentNode.getUserObject; 问题文本=问题节点信息; txtQuestion.setTextquestionText.getQuestion; } }};
您似乎将字符串rootQ.toString作为DefaultMutableTreeNode的参数传递。请仅使用rootQ作为构造函数参数,因为您需要从同一个参数中获取qText。@Ajay我在没有toString的情况下尝试了此操作,并发现它返回树上的整个对象属性。所以我树上的一个节点将有name、id和questionworks,很好,现在发现我在向节点添加子节点时也有一个toString