如何将 JEditorPane 插入 JTable 单元格?

发布于 2024-10-19 18:34:05 字数 2535 浏览 1 评论 0原文

我想将 JEditorPane 放入 JTable 单元格中。 我已经写了这个:

jTabel1.setDefaultRenderer(String.class, new StringEditorPane());
.........
class StringEditorPane extends JEditorPane
    implements TableCellRenderer {

public StringEditorPane() {
    setContentType("text/html");
}



 public Component getTableCellRendererComponent(
            JTable table, Object styledString,
            boolean isSelected, boolean hasFocus,
            int row, int column) {
        if (isSelected) {
            String styledText = (String) styledString;
            setText(styledText);
            setBackground(getSelectionColor());
            return this;
        } else {
            String styledText = (String) styledString;
            setText(styledText);
            setBackground(Color.white);
            return this;
        }
    }
}

我可以编译它并显示 JEditorPane 但它也会引发异常,我不知道为什么。

例外:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at javax.swing.text.DefaultStyledDocument.getParagraphElement(DefaultStyledDocument.java:445)
    at javax.swing.text.StyledEditorKit$AttributeTracker.updateInputAttributes(StyledEditorKit.java:223)
    at javax.swing.text.StyledEditorKit$AttributeTracker.caretUpdate(StyledEditorKit.java:258)
    at javax.swing.text.JTextComponent.fireCaretUpdate(JTextComponent.java:391)
    at javax.swing.text.JTextComponent$MutableCaretEvent.fire(JTextComponent.java:4387)
    at javax.swing.text.JTextComponent$MutableCaretEvent.stateChanged(JTextComponent.java:4409)
    at javax.swing.text.DefaultCaret.fireStateChanged(DefaultCaret.java:782)
    at javax.swing.text.DefaultCaret.changeCaretPosition(DefaultCaret.java:1257)
    at javax.swing.text.DefaultCaret.handleSetDot(DefaultCaret.java:1153)
    at javax.swing.text.DefaultCaret.setDot(DefaultCaret.java:1134)
    at javax.swing.text.DefaultCaret$Handler.removeUpdate(DefaultCaret.java:1777)
    at javax.swing.text.AbstractDocument.fireRemoveUpdate(AbstractDocument.java:243)
    at javax.swing.text.AbstractDocument.handleRemove(AbstractDocument.java:608)
    at javax.swing.text.AbstractDocument.remove(AbstractDocument.java:576)
    at javax.swing.JEditorPane.setText(JEditorPane.java:1493)
    at myProject.StringEditorPane.getTableCellRendererComponent(MainWindow.java:136)
    at javax.swing.JTable.prepareRenderer(JTable.java:5720)
........

MainWindow.java 中的第 136 行:

setText(styledText);

来自 getTableCellRendererComponent 函数。

I want to put JEditorPane in JTable cell.
I've written this:

jTabel1.setDefaultRenderer(String.class, new StringEditorPane());
.........
class StringEditorPane extends JEditorPane
    implements TableCellRenderer {

public StringEditorPane() {
    setContentType("text/html");
}



 public Component getTableCellRendererComponent(
            JTable table, Object styledString,
            boolean isSelected, boolean hasFocus,
            int row, int column) {
        if (isSelected) {
            String styledText = (String) styledString;
            setText(styledText);
            setBackground(getSelectionColor());
            return this;
        } else {
            String styledText = (String) styledString;
            setText(styledText);
            setBackground(Color.white);
            return this;
        }
    }
}

I can compile it and it show JEditorPane but it also throws an exception and I don't know why.

Exception:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at javax.swing.text.DefaultStyledDocument.getParagraphElement(DefaultStyledDocument.java:445)
    at javax.swing.text.StyledEditorKit$AttributeTracker.updateInputAttributes(StyledEditorKit.java:223)
    at javax.swing.text.StyledEditorKit$AttributeTracker.caretUpdate(StyledEditorKit.java:258)
    at javax.swing.text.JTextComponent.fireCaretUpdate(JTextComponent.java:391)
    at javax.swing.text.JTextComponent$MutableCaretEvent.fire(JTextComponent.java:4387)
    at javax.swing.text.JTextComponent$MutableCaretEvent.stateChanged(JTextComponent.java:4409)
    at javax.swing.text.DefaultCaret.fireStateChanged(DefaultCaret.java:782)
    at javax.swing.text.DefaultCaret.changeCaretPosition(DefaultCaret.java:1257)
    at javax.swing.text.DefaultCaret.handleSetDot(DefaultCaret.java:1153)
    at javax.swing.text.DefaultCaret.setDot(DefaultCaret.java:1134)
    at javax.swing.text.DefaultCaret$Handler.removeUpdate(DefaultCaret.java:1777)
    at javax.swing.text.AbstractDocument.fireRemoveUpdate(AbstractDocument.java:243)
    at javax.swing.text.AbstractDocument.handleRemove(AbstractDocument.java:608)
    at javax.swing.text.AbstractDocument.remove(AbstractDocument.java:576)
    at javax.swing.JEditorPane.setText(JEditorPane.java:1493)
    at myProject.StringEditorPane.getTableCellRendererComponent(MainWindow.java:136)
    at javax.swing.JTable.prepareRenderer(JTable.java:5720)
........

And the 136 line from MainWindow.java:

setText(styledText);

from getTableCellRendererComponent function.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

卖梦商人 2024-10-26 18:34:05

看来崩溃是由于删除旧内容而引发的。
尝试用新实例替换文档。在 setText() 调用之前

setDocument(getEditorKit().createDefaultDocument());

It seems the crash is thrown from removing old content.
Try to replace document with a new instance. Before setText() call

setDocument(getEditorKit().createDefaultDocument());
宛菡 2024-10-26 18:34:05

看起来您需要处理 styledText 变量的 null 值。

Looks like you need to handle a null value for your styledText variable.

时光沙漏 2024-10-26 18:34:05

不确定为什么需要自定义渲染器。 String 类的默认呈现器是 JLabel,并且 JLabels 支持 HTML。

Not sure why you need a custom renderer. The default renderer for the String class is a JLabel and JLabels support HTML.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文