使用自定义树模型添加Swing树选择侦听器

时间:2012-03-18 20:10:32

标签: java swing jtree event-listener treemodel

我现在正在研究java,我的一个应用程序是简单的Swing文件层次结构查看器,它使用JTree widget.My问题是如何添加Jtree鼠标选择事件监听器(例如将节点文本值记录到控制台)如果我以这种方式实现了TreeModel(例如“书中的Java Foundation Classes in a Nutshell”):

    public class FileTreeDemo {
    public static void main(String[] args) {
        File root;
        if (args.length > 0)
            root = new File(args[0]);
        else
            root = new File(System.getProperty("user.home"));

                FileTreeModel model = new FileTreeModel(root);

        MyJtree tree = new MyJtree();
        tree.setModel(model);

        JScrollPane scrollpane = new JScrollPane(tree);

        JFrame frame = new JFrame("FileTreeDemo");
        frame.getContentPane().add(scrollpane, "Center");
        frame.setSize(400, 600);
        frame.setVisible(true);
    }
}

class FileTreeModel implements TreeModel {
    protected File root;

    public FileTreeModel(File root) {
        this.root = root;
    }

    public Object getRoot() {
        return root;
    }

    public boolean isLeaf(Object node) {
        return ((File) node).isFile();
    }

    public int getChildCount(Object parent) {
        String[] children = ((File) parent).list();
        if (children == null)
            return 0;
        return children.length;
    }

    public Object getChild(Object parent, int index) {
        String[] children = ((File) parent).list();
        if ((children == null) || (index >= children.length))
            return null;
        return new File((File) parent, children[index]);
    }

    public int getIndexOfChild(Object parent, Object child) {
        String[] children = ((File) parent).list();
        if (children == null)
            return -1;
        String childname = ((File) child).getName();
        for (int i = 0; i < children.length; i++) {
            if (childname.equals(children[i]))
                return i;
        }
        return -1;
    }

    public void valueForPathChanged(TreePath path, Object newvalue) {
    }

    public void addTreeModelListener(TreeModelListener l) {
    }

    public void removeTreeModelListener(TreeModelListener l) {
    }
}

这里我试图通过MyJtree扩展JTree类并添加AddTreeSelectionListener

public class MyJtree extends JTree {
    public MyJtree() {
        super();
        this.addTreeSelectionListener(new TreeSelectionListener() {
            public void valueChanged(TreeSelectionEvent e) {
                DefaultMutableTreeNode node = (DefaultMutableTreeNode) e
                        .getPath().getLastPathComponent();
                System.out.println("You selected " + node);
            }
        });
    }
}

但是我点击了JTree项目,我明白了:

  

线程“AWT-EventQueue-0”中的异常java.lang.ClassCastException:   java.io.File无法强制转换为javax.swing.tree.DefaultMutableTreeNode

那么,我该如何解决这个问题?

2 个答案:

答案 0 :(得分:4)

未在您的听众中投放到DefaultMutableTreeNodegetLastPathComponent方法返回TreeModel中的元素,在您的情况下为File

这个

上的堆栈跟踪和异常消息非常清楚

答案 1 :(得分:3)

由于模型包含File对象,e.getPath()。getLastPathComponent()返回File对象(只是模型返回的对象)。 另外,为避免ClassCastException,您可能需要检查返回的Object是否为您期望的精确类。

Object object = e.getPath().getLastPathComponent(); 
if (object instanceof File){ 
    File file = (File) object; 
}