包含HTML代码的JLabel节点的JTree在扩展节点时会引发异常

时间:2014-01-29 12:30:02

标签: java swing exception jlabel jtree

我创建了一个以JLabel为对象的JTree。 包含HTML的JLabel。 例如:

new JLabel("<html><FONT COLOR=ORANGE size=\"2\"><B>Unknown Http Response Status</B></FONT></html>")

我使用了这个Cell渲染器:

public class TreeWithJLabelCellRenderer extends DefaultTreeCellRenderer {
    private JLabel label;

    public TreeWithJLabelCellRenderer() {
        label = new JLabel();
    }

    public Component getTreeCellRendererComponent(JTree tree,
                                                  Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
        Object nodeValue = ((DefaultMutableTreeNode) value).getUserObject();

        label=(JLabel)nodeValue;

        return label;
    }
}

我的问题 - 当包含JTree的JFrame出现并且我尝试扩展树节点时,我得到了这个异常:

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: javax.swing.JLabel cannot be cast to javax.swing.text.JTextComponent
    at javax.swing.text.html.EditableView.paint(EditableView.java:76)
    at javax.swing.text.BoxView.paintChild(BoxView.java:161)
    at javax.swing.text.BoxView.paint(BoxView.java:433)
    at javax.swing.text.BoxView.paintChild(BoxView.java:161)
    at javax.swing.text.BoxView.paint(BoxView.java:433)
    at javax.swing.text.ParagraphView.paint(ParagraphView.java:587)
    at javax.swing.text.html.ParagraphView.paint(ParagraphView.java:233)
    at javax.swing.text.BoxView.paintChild(BoxView.java:161)
    at javax.swing.text.BoxView.paint(BoxView.java:433)
    at javax.swing.text.html.BlockView.paint(BlockView.java:282)
    at javax.swing.text.BoxView.paintChild(BoxView.java:161)
    at javax.swing.text.BoxView.paint(BoxView.java:433)
    at javax.swing.text.html.BlockView.paint(BlockView.java:282)
    at javax.swing.plaf.basic.BasicHTML$Renderer.paint(BasicHTML.java:481)
    at com.seaglasslookandfeel.util.SeaGlassGraphicsUtils.paintText(SeaGlassGraphicsUtils.java:162)
    at com.seaglasslookandfeel.ui.SeaGlassLabelUI.paint(SeaGlassLabelUI.java:163)
    at com.seaglasslookandfeel.ui.SeaGlassLabelUI.update(SeaGlassLabelUI.java:146)
    at javax.swing.JComponent.paintComponent(JComponent.java:778)
    at javax.swing.JComponent.paint(JComponent.java:1054)
    at javax.swing.CellRendererPane.paintComponent(CellRendererPane.java:151)
    at javax.swing.plaf.basic.BasicTreeUI.paintRow(BasicTreeUI.java:1539)
    at gui.HttpRequesterTool.TreeWithJLabelUI.paintRow(TreeWithJLabelUI.java:18)
    at javax.swing.plaf.basic.BasicTreeUI.paint(BasicTreeUI.java:1223)
    at javax.swing.plaf.ComponentUI.update(ComponentUI.java:161)
    at javax.swing.JComponent.paintComponent(JComponent.java:778)
    at javax.swing.JComponent.paint(JComponent.java:1054)
    at javax.swing.JComponent.paintChildren(JComponent.java:887)
    at javax.swing.JComponent.paint(JComponent.java:1063)
    at javax.swing.JViewport.paint(JViewport.java:731)
    at javax.swing.JComponent.paintChildren(JComponent.java:887)
    at javax.swing.JComponent.paint(JComponent.java:1063)
    at javax.swing.JComponent.paintChildren(JComponent.java:887)
    at javax.swing.JComponent.paint(JComponent.java:1063)
    at javax.swing.JComponent.paintChildren(JComponent.java:887)
    at javax.swing.JComponent.paint(JComponent.java:1063)
    at javax.swing.JLayeredPane.paint(JLayeredPane.java:585)
    at javax.swing.JComponent.paintChildren(JComponent.java:887)
    at javax.swing.JComponent.paint(JComponent.java:1063)
    at javax.swing.JComponent.paintToOffscreen(JComponent.java:5219)
    at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1529)
    at javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1452)
    at javax.swing.RepaintManager.paint(RepaintManager.java:1249)
    at javax.swing.JComponent._paintImmediately(JComponent.java:5167)
    at javax.swing.JComponent.paintImmediately(JComponent.java:4978)
    at javax.swing.RepaintManager$3.run(RepaintManager.java:808)
    at javax.swing.RepaintManager$3.run(RepaintManager.java:796)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:796)
    at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:769)
    at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:718)
    at javax.swing.RepaintManager.access$1100(RepaintManager.java:62)
    at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1677)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:733)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:694)
    at java.awt.EventQueue$3.run(EventQueue.java:692)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:703)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

我做错了什么?

1 个答案:

答案 0 :(得分:0)

您收到ClassCastException,因为getUserObject() 最初将返回String,而JLabel无法投放到setText(...)

您可以使用DefaultMutableTreeNode node = (DefaultMutableTreeNode) value; String text = ((String) node.getUserObject()); String newText = "<html><FONT COLOR=ORANGE size=\\\"2\\\"><B>" + text + "</B></FONT></html>"; setText(newText); return this; 替换文字。像这样的东西

import java.awt.Component;
import javax.swing.JFrame;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;

public class TreeCellRender {

    JTree tree = new JTree();

    public TreeCellRender() {
        DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer() {
            @Override
            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);

                DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
                String text = ((String) node.getUserObject());
                String newText = "<html><FONT COLOR=ORANGE size=\\\"2\\\"><B>" + text + "</B></FONT></html>";
                setText(newText);
                return this;
            }
        };

        tree.setCellRenderer(renderer);

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(tree);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new TreeCellRender();
            }
        });
    }
}

enter image description here

public Component getTreeCellRendererComponent(....) {
    ....
    setFont(new Font("impact", Font.PLAIN, 20));
    setForeground(Color.GREEN);
    return this;
}

另外,如果您只想设置字体和颜色,您可以在方法中轻松完成此操作

public class MyTreeCellRenderer extends DefaultTreeCellRenderer {
    DefaultTreeCellRenderer defaultTreeCellRenderer=new DefaultTreeCellRenderer();

    @Override
    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);

        DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
        String text = (String)node.getUserObject();
        JTextPane textPane = new JTextPane();
        textPane.setContentType("text/html");
        textPane.setText(text);
        return textPane;
    }
}

enter image description here


<强>更新

原始海报提出的另一种方式是

  

另一种方法:使用包含html代码的字符串节点创建树。渲染器将​​是这样的:

{{1}}