Java: Why doesn't JTable use TableCellEditor?

The purpose of MyTableCellEditor is to make the JTable cell behave like an Excel cell, IOW, enter a value after a single click in the cell, instead of joining an existing value. I did not expect the following code to work in the first pass, but I expected to get remote debugging points in getTableCellEditorComponent and getCellEditorValue. Why isn't getTableCellEditorComponent or getCellEditorValue called when using jTable?

public class MyTable extends javax.swing.JFrame implements TableModelListener {
    private static final MyTableCellEditor tableCellEditor =
        new MyTableCellEditor();
        ...
    public MyTable() {
        initComponents();
        jTable.getModel().addTableModelListener(MyTable.this);
        ...
    private void initComponents() { // Generated by the Form Editor.
        jTable = new javax.swing.JTable();
        jTable.setCellEditor(tableCellEditor);
        ...
public class MyTableCellEditor extends AbstractCellEditor implements
        TableCellEditor {
    JComponent component = new JTextField();
    public Component getTableCellEditorComponent(JTable table, Object value,
            boolean isSelected, int rowIndex, int vColIndex) {
        if (isSelected) {
            ((JTextField)component).selectAll();
        }
        ((JTextField)component).setText((String)value);
        return component;
    }
    public Object getCellEditorValue() {
        return ((JTextField)component).getText();
    }
}
+3
source share
1 answer

setCellEditor(TableCellEditor) ( , . JTable.setDefaultEditor(), .

jTable.setDefaultEditor(String.class, tableCellEditor);

TableColumnModel,

jTable.getColumnModel().getColumn(1).setCellEditor(tableCellEditor);
+7

Source: https://habr.com/ru/post/1772647/


All Articles