从CellEditorListener获取编辑的TreeNode [英] Get Edited TreeNode from a CellEditorListener
问题描述
早些时候,我问了如何在重命名TreeNode时触发一个事件(这里一>)。我的问题回答了,但我遇到了另一个问题。我需要访问在CellEditorListener的editingStopped事件中编辑的TreeNode。这是我必须这样做的代码:
package com.gamecreator;
import javax.swing.event.CellEditorListener;
import javax.swing.event.ChangeEvent;
import javax.swing.tree.DefaultTreeCellEditor;
public class CustomCellEditorListener实现CellEditorListener {
public CustomCellEditorListener(){
}
public void editingCanceled(ChangeEvent e){
}
public void editingStopped(ChangeEvent e){
DefaultTreeCellEditor editor =(DefaultTreeCellEditor)e.getSource(); //这给我错误。
CustomTreeNode node = //我在这里放什么???
node.getResource()。setName((String)node.getUserObject());
//调试
System.out.println(node.getResource()。getName());
}
}
我收到这个错误:
线程中的异常AWT-EventQueue-0java.lang.ClassCastException:
javax.swing.tree.DefaultTreeCellEditor $ 1不能转换为
javax.swing.tree.DefaultTreeCellEditor
编辑:
在另一个尝试中,我在CustomCellEditorListener
public void editingStopped(ChangeEvent e){
TreePath path =((CustomTreeCellEditor)e.getSource())。getLastPath (); //这给我错误。
CustomTreeNode node =(CustomTreeNode)path.getLastPathComponent();
node.getResource()。setName((String)node.getUserObject());
//调试
System.out.println(node.getResource()。getName());
}
而CustomTreeCellEditor中的此代码
public TreePath getLastPath(){
return lastPath;
}
我有同样的错误(我预计我会的)。我应该工作,所以剩下的唯一真正的问题是,为什么我得到错误,我该怎么解决?但是如果有更好的方法来完成这个,我愿意听。 p>
编辑2:
我已经做了一个小小的例子,我正在尝试完成,可以找到这里(这是一个Eclipse档案)。
我发现一个解决方案其实很简单。当TreeNode重命名时,它最终成为树中唯一选定的节点。因此,我可以使用:
CustomTreeNode node =(CustomTreeNode)tree.getLastSelectedPathComponent();
Earlier I asked how to fire an event when a TreeNode was renamed (here). My question was answered, but I ran into another problem. I need to access the TreeNode that is being edited in the CellEditorListener's editingStopped event. This is the code I have to do so:
package com.gamecreator;
import javax.swing.event.CellEditorListener;
import javax.swing.event.ChangeEvent;
import javax.swing.tree.DefaultTreeCellEditor;
public class CustomCellEditorListener implements CellEditorListener {
public CustomCellEditorListener() {
}
public void editingCanceled(ChangeEvent e) {
}
public void editingStopped(ChangeEvent e) {
DefaultTreeCellEditor editor = (DefaultTreeCellEditor) e.getSource(); //This gives me the error.
CustomTreeNode node = //What do I put here???;
node.getResource().setName((String) node.getUserObject());
//For debugging
System.out.println(node.getResource().getName());
}
}
I get this error:
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: javax.swing.tree.DefaultTreeCellEditor$1 cannot be cast to javax.swing.tree.DefaultTreeCellEditor
EDIT: In another attempt, I used this code in the CustomCellEditorListener
public void editingStopped(ChangeEvent e) {
TreePath path = ((CustomTreeCellEditor) e.getSource()).getLastPath(); //This gives me the error.
CustomTreeNode node = (CustomTreeNode) path.getLastPathComponent();
node.getResource().setName((String) node.getUserObject());
//For debugging
System.out.println(node.getResource().getName());
}
and this code in the CustomTreeCellEditor
public TreePath getLastPath() {
return lastPath;
}
I got the same error (I expected I would). What I have should work, so the only real question remaining is, "Why am I getting the error and how can I fix it?," but if anyone has a better way to accomplish this, I'm willing to listen.
EDIT 2: I have made a small example of what I'm trying to accomplish that can be found here (It's an Eclipse archive).
I found a solution that was actually very simple. When a TreeNode is renamed, it ends up being the only selected node in the tree. Because of that, I was able to use:
CustomTreeNode node = (CustomTreeNode) tree.getLastSelectedPathComponent();
这篇关于从CellEditorListener获取编辑的TreeNode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!