如何将 JEditorPane 插入 JTable 单元格?
我想将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看来崩溃是由于删除旧内容而引发的。
尝试用新实例替换文档。在 setText() 调用之前
It seems the crash is thrown from removing old content.
Try to replace document with a new instance. Before setText() call
看起来您需要处理
styledText
变量的null
值。Looks like you need to handle a
null
value for yourstyledText
variable.不确定为什么需要自定义渲染器。 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.