开发者

How to insert JEditorPane into JTable cell?

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.AbstractDocumen开发者_如何学编程t.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.


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

setDocument(getEditorKit().createDefaultDocument());


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜