从 CellEditorListener 获取编辑的 TreeNode [英] Get Edited TreeNode from a CellEditorListener

查看:26
本文介绍了从 CellEditorListener 获取编辑的 TreeNode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

之前我问过如何在重命名 TreeNode 时触发事件(此处一>).我的问题得到了回答,但我遇到了另一个问题.我需要访问在 Ce​​llEditorListener 的 editStopped 事件中正在编辑的 TreeNode.这是我必须这样做的代码:

package com.gamecreator;导入 javax.swing.event.CellEditorListener;导入 javax.swing.event.ChangeEvent;导入 javax.swing.tree.DefaultTreeCellEditor;公共类 CustomCellEditorListener 实现 CellEditorListener {公共 CustomCellEditorListener() {}公共无效编辑取消(ChangeEvent e){}公共无效编辑停止(ChangeEvent e){DefaultTreeCellEditor editor = (DefaultTreeCellEditor) e.getSource();//这给了我错误.CustomTreeNode node =//我在这里放什么???;node.getResource().setName((String) node.getUserObject());//用于调试System.out.println(node.getResource().getName());}}

我收到此错误:

<块引用>

线程AWT-EventQueue-0"中的异常java.lang.ClassCastException:javax.swing.tree.DefaultTreeCellEditor$1 不能转换为javax.swing.tree.DefaultTreeCellEditor

在另一个尝试中,我在 CustomCellEditorListener 中使用了这段代码

public void editoringStopped(ChangeEvent e) {树路径路径 = ((CustomTreeCellEditor) e.getSource()).getLastPath();//这给了我错误.CustomTreeNode 节点 = (CustomTreeNode) path.getLastPathComponent();node.getResource().setName((String) node.getUserObject());//用于调试System.out.println(node.getResource().getName());}

以及 CustomTreeCellEditor 中的这段代码

public TreePath getLastPath() {返回最后一条路径;}

我遇到了同样的错误(我希望我会).我所拥有的应该可以工作,所以剩下的唯一真正的问题是,为什么我会收到错误,我该如何解决它?"但如果有人有更好的方法来实现这一点,我愿意倾听.

编辑 2:我做了一个小例子来说明我想要完成的事情,可以在 here (这是一个 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆