Editing table cells

You can configure a JViews TGO table component to support cell editing. The configuration consists in defining specific cell editors (javax.swing.CellEditor) for the JViews TGO business attributes (IlpAttribute) to be edited. The Table model (IlpTableModel) must be extended to indicate which cells are editable, and the Table view (IlpTableView) must be extended to indicate the cell editors to be used for a given TGO attribute.
To configure the table component for cell editing:
  1. Define and implement the cell editors for the attributes you want to edit.
    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; 
      } 
    } 
    
  2. Override the method isCellEditable(int,int) in IlpTableListModel to return 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; 
      } 
    } 
    
  3. Override the method getCellEditor(IlpRepresentationObject,IlpAttribute) in IlpTableView to return the appropriate cell editor for the given attribute.
    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); 
      } 
    } 
    
  4. Configure IlpTable with the custom Table Model and Table View defined in steps 2 and 3.
    After you have defined the table model (EditableTableListModel) and the table view (EditableTableView), you must set them to a table component in order to be able to edit the cells. To do so, you use the methods setModel() and setView(), as follows.
    table = new IlpTable(); 
    // Install custom types that allow for the use of table cell editors 
    table.setModel(new EditableTableListModel()); 
    table.setView(new EditableTableView()); 
When the application is running, double-clicking the Name Attribute column will activate the NameAttributeEditor.