我在上面创建了一个jTable和TableCellRenderer,TableCellEditor。我需要在那里放置editable(使用text / html上下文类型)JEditorPane。当我在里面写一些文字并调整组件大小时,文字会消失。我做错了什么?此外,在这个组件上方,我有按钮文本版本:例如:
JButton bold = new JButton():
bold.setAction(new StyledEditorKit.BoldAction());
这是我自定义模型的一部分:
private JEditorPane editorTxtPane = new JEditorPane("text/html", "");
private JEditorPane rendererTxtPane = new JEditorPane("text/html", "");
private final JPanel editorPanel = new JPanel();
private final JPanel rendererPanel = new JPanel();
private final ArrayList<FocusListener> editorFocusListeners = new ArrayList<FocusListener>();
public SampleModel() {
super();
rendererTxtPane.setContentType("text/html");
editorTxtPane.setContentType("text/html");
rendererPanel.add(initCellControls(rendererPanel, rendererLabel));
rendererPanel.add(rendererTxtPane);
editorPanel.add(initCellControls(editorPanel, editorLabel));
JScrollPane sp = new JScrollPane(editorTxtPane);
sp.setBorder(null);
editorPanel.add(sp);
editorTxtPane.addFocusListener(new FocusAdapter() {
@Override
public void focusGained(FocusEvent e) {
super.focusGained(e);
e.setSource(editorTxtPane);
for (int i = editorFocusListeners.size() - 1; i >= 0; i--) {
editorFocusListeners.get(i).focusGained(e);
}
}
@Override
public void focusLost(FocusEvent e) {
super.focusLost(e);
e.setSource(editorTxtPane);
for (int i = editorFocusListeners.size() - 1; i >= 0; i--) {
editorFocusListeners.get(i).focusLost(e);
}
}
});
}
这是我的编辑器和渲染器方法:
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
Comment c = data.get(row);
rendererTxtPane.setText(c.getComment());
return rendererPanel;
}
@Override
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
Comment c = data.get(row);
c.setNeedSave(true);
editorTxtPane.setText(c.getComment());
return editorPanel;
}
答案 0 :(得分:1)
<{3}}的工作原理 。特别是,编辑器仅在编辑单元格时有效。您的TableModel
应存储每一行Document
。编辑结束后,您的模型将使用修订后的Document
进行更新,如Editors and Renderers所述。您可以将自己的行为与here进行比较,这可能构成example的基础。
答案 1 :(得分:0)
这可能是以下两个条件之一的结果: