Creating a Custom Table Cell Editor in a JTable Component
import java.awt.Component;
import javax.swing.AbstractCellEditor;
import javax.swing.JComponent;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableColumn;
public class Main {
public static void main(String[] argv) throws Exception {
JTable table = new JTable();
TableColumn col = table.getColumnModel().getColumn(0);
col.setCellEditor(new MyTableCellEditor());
}
}
class MyTableCellEditor extends AbstractCellEditor implements TableCellEditor {
JComponent component = new JTextField();
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected,
int rowIndex, int vColIndex) {
((JTextField) component).setText((String) value);
return component;
}
public Object getCellEditorValue() {
return ((JTextField) component).getText();
}
}
Related examples in the same category