I have a program that adds questions to a jtree. Each question on the Jtree is of type Question which has 3 properties; name, id and questionText. I want my Jtree to display only the question name but when the node is selected, it should show the questionText in a textbox.
我有一个程序将问题添加到jtree中。Jtree上的每个问题都有三个属性的类型问题;名称、id和questionText。我想要我的Jtree只显示问题名称,但是当选择节点时,它应该在一个文本框中显示问题文本。
The problem is the Jtree displays the question's name as I want it to but, when I select a node of the tree I get a ClassCastException. This is because of the getUserObject() in my treeSelectionListener method. It gets a String returned by getUserObject() and tries to cast it to a Question class. How do I get the selected String's Question class properties like questionText or id without getUserObject() or is there another way to represent this tree?
问题是Jtree显示问题的名称,我想要它,但是,当我选择树的一个节点时,我得到一个ClassCastException。这是因为我的treeSelectionListener方法中的getUserObject()。它得到getUserObject()返回的字符串,并尝试将其转换为一个问题类。我如何获得所选字符串的问题类属性,如疑问文本或id没有getUserObject(),或者是否有另一种表示这棵树的方法?
Here is my code:
这是我的代码:
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; }
}
Parts of the code to create the tree:
创建树的部分代码:
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;
}
1 个解决方案
#1
0
Like @Ajay hinted with his comment, instead of
就像@Ajay所暗示的,而不是。
DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(createdQuestion.toString());
simply do
简单的做
DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(createdQuestion);
You are the one currently setting your user objects to Strings, not the API.
您是当前将用户对象设置为字符串的对象,而不是API。
Also, you can get the selected node through tree.getLastSelectedPathComponent()
instead of always getting selection path and then calling TreePath.getLastPathComponent()
.
另外,您还可以通过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());
}
}};
#1
0
Like @Ajay hinted with his comment, instead of
就像@Ajay所暗示的,而不是。
DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(createdQuestion.toString());
simply do
简单的做
DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(createdQuestion);
You are the one currently setting your user objects to Strings, not the API.
您是当前将用户对象设置为字符串的对象,而不是API。
Also, you can get the selected node through tree.getLastSelectedPathComponent()
instead of always getting selection path and then calling TreePath.getLastPathComponent()
.
另外,您还可以通过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());
}
}};