这是问题所在。我有以下类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)

最佳答案

如果没有堆栈跟踪转储,就很难看清问题出在哪里,但是假设它在您发布的代码中,我猜想它可能是您的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;
    }
}


此更改将在执行强制转换之前检查valueFileNode。如果不是FileNode,它将以黑色绘制该值,而不是此时崩溃。

通常,在执行强制转换之前进行检查是一种很好的做法,因此可以在对象实际上不可强制转换的情况下正确处理它。

关于java - Linux中但不是Windows中的ClassCastException-DefaultMutableTreeNode,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17359153/

10-12 04:52