1

这是问题所在。我有以下类 FileNode 扩展 DefaultMutableTreeNode

import javax.swing.tree.DefaultMutableTreeNode;
import java.io.File;

public class FileNode extends  DefaultMutableTreeNode {
    public FileNode(String Name, File File) {
        super(Name);
        this.Name = Name;
        this.File = File;
        this.isFolder = false;
        CheckedOut = false;
    }

    public FileNode(String Name, File File, Boolean isFolder) {
        super(Name);
        this.Name = Name;
        this.File = File;
        this.isFolder = isFolder;
        CheckedOut = false;
    }

    public Boolean CheckedOut;
    public File File;
    public String Name;
    public Boolean isFolder;
}

我有一棵树,里面装满了这些节点。

我收到以下异常:

private class FileTreeCellRenderer extends DefaultTreeCellRenderer {
    public Component getTreeCellRendererComponent(JTree tree, Object value,
          boolean sel, boolean expanded, boolean leaf,
          int row, boolean hasFocus) {
        super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf,
           row, hasFocus);
        FileNode fn = (FileNode)value;
        Color nodeColor = Color.BLACK;
        if (!fn.isFolder) {
            if (fn.CheckedOut) {
              nodeColor = Color.RED;
            } else {
              nodeColor = new Color(0,100,0);
            }
        }
        setForeground(nodeColor);
        return this;
    }
}

奇怪的是,我只在 Linux 中而不是在 Windows 中得到这个错误。我在两个平台上运行相同的 JRE - Oracle JDK 1.7.0_25

有人对我做错了什么有任何想法吗?

这是堆栈跟踪:

Exception in thread "main" java.lang.ClassCastException: javax.swing.tree.DefaultMutableTreeNode cannot be cast to dropboxcheckout.FileNode
    at dropboxcheckout.CheckoutPanel$FileTreeCellRenderer.getTreeCellRendererComponent(CheckoutPanel.java:115)
    at javax.swing.plaf.basic.BasicTreeUI$NodeDimensionsHandler.getNodeDimensions(BasicTreeUI.java:2786)
    at javax.swing.tree.AbstractLayoutCache.getNodeDimensions(AbstractLayoutCache.java:492)
    at javax.swing.tree.VariableHeightLayoutCache$TreeStateNode.updatePreferredSize(VariableHeightLayoutCache.java:1359)
    at javax.swing.tree.VariableHeightLayoutCache.updateNodeSizes(VariableHeightLayoutCache.java:917)
    at javax.swing.tree.VariableHeightLayoutCache.invalidateSizes(VariableHeightLayoutCache.java:371)
    at javax.swing.plaf.basic.BasicTreeUI.setCellRenderer(BasicTreeUI.java:385)
    at javax.swing.plaf.basic.BasicTreeUI$Handler.propertyChange(BasicTreeUI.java:3390)
    at java.beans.PropertyChangeSupport.fire(PropertyChangeSupport.java:335)
    at java.beans.PropertyChangeSupport.firePropertyChange(PropertyChangeSupport.java:327)
    at java.beans.PropertyChangeSupport.firePropertyChange(PropertyChangeSupport.java:263)
    at java.awt.Component.firePropertyChange(Component.java:8382)
    at javax.swing.JTree.setCellRenderer(JTree.java:759)
    at dropboxcheckout.CheckoutPanel.<init>(CheckoutPanel.java:85)
    at dropboxcheckout.CheckoutFrame.<init>(CheckoutFrame.java:37)
4

1 回答 1

2

如果没有堆栈跟踪转储,很难看出问题出在哪里,但假设它在您发布的代码中,我猜它可能是您的FileNode演员。

我的建议是正确处理这个异常,不管它在什么系统上运行......

private class FileTreeCellRenderer extends DefaultTreeCellRenderer {
    public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
        super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);

        Color nodeColor = Color.BLACK;

        if (value instanceof FileNode){
            FileNode fn = (FileNode)value;
            if (!fn.isFolder) {
                if (fn.CheckedOut) {
                    nodeColor = Color.RED;
                }
                else {
                    nodeColor = new Color(0,100,0);
                }
            }
        }

        setForeground(nodeColor);
        return this;
    }
}

此更改将在执行强制转换之前检查是否value为 a 。FileNode如果它不是 a FileNode,它会将值绘制为黑色,而不是此时崩溃。

一般来说,在进行强制转换之前进行检查是一种很好的做法,这样您就可以在对象实际上无法强制转换的情况下正确处理它。

于 2013-06-28T07:19:57.457 回答