I have a JTable that contains cells which need to be JTextPanes with the
StyledDocument set to a custom one that I have written. I have created a
cell editor and renderer that does this but unfortunately this only creates
one instance of an EditorTextPane (subclass of JTextPane) for all of the
cells and just changes the value of the text within. This means that all the
cells in all rows are set to the same size when I set the size of just one
EditorTextPane as this is in effect the same EditorTextPane as all the rest
of the cells.
My question is, how can I go about creating a renderer and editor that uses
a different EditorTextPane for each cell of the table so resizing can be
done separately for all.
Below is included the editor/renderer class and also how I assign these to
the JTable.
Thanks
import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.text.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.util.*;
public class EditorCellRenderer extends AbstractCellEditor implements
TableCellEditor, TableCellRenderer {
private EditorTextPane cellText = new EditorTextPane();
protected EventListenerList listenerList = new EventListenerList();
protected ChangeEvent changeEvent = new ChangeEvent(this);
public EditorCellRenderer(EditorTextPane secPane, JPanel tablePanel, JTable
table, JScrollPane tableScroll) {
//set up the text pane with the correct document
cellText.getDocument().addUndoableEditListener(new
EditorUndoableEditListener(Editor.undoAction, Editor.redoAction,
Editor.undo));
SimpleAttributeSet attrs = new SimpleAttributeSet();
StyleConstants.setFontFamily(attrs, "SansSerif");
StyleConstants.setFontSize(attrs, 12);
cellText.setCharacterAttributes(attrs, false);
TrackChangesStyledDocument trackDoc = new
TrackChangesStyledDocument(cellText, secPane, tablePanel, table,
tableScroll);
cellText.setStyledDocument(trackDoc);
cellText.setBorder(BorderFactory.createLineBorder(Color.black));
}
public void addCellEditorListener(CellEditorListener listener) {
listenerList.add(CellEditorListener.class, listener);
}
public void removeCellEditorListener(CellEditorListener listener) {
listenerList.remove(CellEditorListener.class, listener);
}
protected void fireEditingStopped() {
CellEditorListener listener;
Object[] listeners = listenerList.getListenerList();
for (int i = 0; i < listeners.length; i++) {
if (listeners[i] == CellEditorListener.class) {
listener = (CellEditorListener) listeners[i + 1];
listener.editingStopped(changeEvent);
}
}
}
protected void fireEditingCanceled() {
CellEditorListener listener;
Object[] listeners = listenerList.getListenerList();
for (int i = 0; i < listeners.length; i++) {
if (listeners[i] == CellEditorListener.class) {
listener = (CellEditorListener) listeners[i + 1];
listener.editingCanceled(changeEvent);
}
}
}
public void cancelCellEditing() {
fireEditingCanceled();
}
public boolean stopCellEditing() {
fireEditingStopped();
return true;
}
public boolean isCellEditable(EventObject event) {
return true;
}
public boolean shouldSelectCell(EventObject event) {
return true;
}
public Object getCellEditorValue() {
return cellText.getText();
}
public Component getTableCellEditorComponent(JTable table, Object value,
boolean isSelected, int row, int column) {
if (value != null) {
cellText.setText(value.toString());
} else {
cellText.setText("");
}
return cellText;
}
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus, int row, int column) {
if (value != null) {
cellText.setText(value.toString());
} else {
cellText.setText("");
}
return cellText;
}
}
code to set the renderer/editor for the table:
table.setDefaultEditor(Object.class, new EditorCellRenderer(secPane,
tablePanel, table, tableScroll));
table.setDefaultRenderer(Object.class, new EditorCellRenderer(secPane,
tablePanel, table, tableScroll));
Carl
Carl Austin - 30 Mar 2004 16:10 GMT
Please ignore this question, bad day today and wasn't thinking straight. The
simple answer is just to store each JTextPane within the Table model and
write a renderer and editor that displays them correctly. Easy!
Carl
> I have a JTable that contains cells which need to be JTextPanes with the
> StyledDocument set to a custom one that I have written. I have created a
[quoted text clipped - 166 lines]
>
> Carl