我想把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功能。
哦......那麼簡單。我不知道爲什麼我以前沒有注意到。 :/ Thx – latata 2011-02-28 20:54:35
對不起,但它是不正確的。我已將該行更改爲:setText(「xxxx」);併發生同樣的情況 – latata 2011-02-28 21:12:08