tags:

views:

44

answers:

1

Posted CellEditor.

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;

public class UserCellEditor extends AbstractCellEditor 
                            implements TableCellEditor {

    JComponent component = new JTextField();

    public Object getCellEditorValue() {
        return ((JTextField) component).getText();
    }

    public Component getTableCellEditorComponent(JTable table, 
            Object value, boolean isSelected, int row, int column) {
        ((JTextField) component).setText((String) value);
        return component;
    }
}

Then I call table method in such way:

UserTable.getColumnModel().getColumn(0).setCellEditor(new UserCellEditor());

The cell is being edited, but the result by pressing the Enter key or move to another cell is not stored, it returns the original value.

Where am I wrong or what I'm doing wrong?

+1  A: 

It looks like you are editing the value in a local JTextField, while your data model is subsequently returning the unchanged value when editing is complete. Your editor needs to update your model, as suggested in this example.

trashgod