I am learning from this oracle tutorial that uses TableDialogEditDemo.java example class
I wrote a custom cell Renderer and Editor for JTable.
I register them to this Oracle TableDialogEditDemo.java class
...
...
//Set up renderer and editor for the Favorite Color column.
table.setDefaultRenderer(Color.class,
new ColorRenderer(true));
table.setDefaultEditor(Color.class,
new ColorEditor());
TableColumn c = table.getColumnModel().getColumn(2);
c.setCellRenderer(new CellStringRenderer()); //My custom Renderer
c.setCellEditor(new CellStringEditor()); // My custom Editor
//Add the scroll pane to this panel.
add(scrollPane);
...
...
(Updated description) When I click on a cell an input dialogue box pops up and that is OK, and when I type a text and click "OK" the cell in the JTable is updated, but text is not displayed/rendered correctly, I have to click on any other cell to make the text content displayed correctly in the cell.
What is the wrong with my code?.
My Renderer
import java.awt.Component;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;
public class CellStringRenderer extends JLabel implements TableCellRenderer
{
public CellStringRenderer()
{
this.setOpaque(true);
}
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
{
String stringValue = (String) value;
this.setText(stringValue);
return this;
}
}
My Editor (Updated)
import java.awt.Component;
import javax.swing.*;
import javax.swing.table.TableCellEditor;
public class CellStringEditor extends AbstractCellEditor
implements TableCellEditor
{
String input;
@Override
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column)
{
if (isSelected)
{
JOptionPane dialog = new JOptionPane();
input = dialog.showInputDialog(null, "new value");
return dialog;
}
return null;
}
@Override
public Object getCellEditorValue()
{
return input;
}
}