public class NameAttributeEditor extends DefaultCellEditor { // Keep reference to Table view private IlpTableView _view; // Constructor initializing the editor as a text field public NameAttributeEditor(IlpTableView view) { super(new JTextField()); _view = view; } // Set attribute when ending editing @Override public boolean stopCellEditing() { if (super.stopCellEditing()) { // Retrieve the object being edited IlpObject object = view.getSelectionModel().getSelectedObject(); // Update the Name attribute accordingly object.setAttributeValue(IltObject.NameAttribute, getCellEditorValue()); return true; } return false; } }
true
for the cells you want to edit.
public class EditableTableListModel extends IlpTableListModel { @Override public boolean isCellEditable(int rowIndex, int columnIndex) { // Indicates that the cell is editable return true; } }
public class EditableTableView extends IlpTableView { // Keep reference to the name editor private NameAttributeEditor _nameEditor; // Constructor: Initialize editor public EditableTableView() { super(); _nameEditor = new NameAttributeEditor(this); } // Return the name editor when applicable @Override public TableCellEditor getCellEditor(IlpRepresentationObject o, IlpAttribute a) { return (a == IltObject.NameAttribute ? _nameEditor : null); } }
table = new IlpTable(); // Install custom types that allow for the use of table cell editors table.setModel(new EditableTableListModel()); table.setView(new EditableTableView());