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