views:

303

answers:

3

Hi SO'ers,

I am creating a app in Java. I need to provide additional behavior when editing of a cell in a JTable. So ideally this will happen when the cell loses focus after editing. Depending upon some post processing I might reset the value of the cell. I tried using a a Cell Editor but it is not giving me the desired behavior.

In the default JTable only when I Double click a cell it becomes editable. But in my implementation of CellEditor the cell becomes editable as soon as it comes into focus.

Here is the code for the My custom CellEditor,

public class ParameterDefinitionEditor 
    extends AbstractCellEditor
    implements TableCellEditor{

    private JTable table;
    private DefaultTableModel defaultTableModel;

public ParameterDefinitionEditor(DefaultTableModel defaultTableModel,
JTable table) { 

     super();
     this.table = table;
     this.defaultTableModel = defaultTableModel;

     TableColumnModel columnModel = table.getColumnModel();
     columnModel.getColumn(0).setCellEditor(this);

}

    public Component getTableCellEditorComponent(JTable table, 
             Object value, 
          boolean isSelected, 
         int row, 
          int column) {

        if (isSelected) {
            // Do some processing.
        } 

        ((JTextField)component).setText((String)value);

        // Return the configured component
        return component;
    }

    public Object getCellEditorValue() {

     return ((JTextField)component).getText();
    }


}

Any help will be appreciated. Thanks.

+1  A: 

I don't think by providing custom cell editor serves your purpose.

If you want to do some processing based on user actions then your table model should have
a set of listeners (that implement TableModelListener) and your logic should be put
in the "tableChanged" method.

Check this section on Swing tutorial as well:
http://java.sun.com/docs/books/tutorial/uiswing/components/table.html

sateesh
+1  A: 

Depending upon some post processing I might reset the value of the cell.

You can do this right in the cell editor if you desire by overriding the stopCellEditing() method.

In the default JTable only when I Double click a cell it becomes editable. But in my implementation of CellEditor the cell becomes editable as soon as it comes into focus.

Extend the DefaultCellEditor. This is controlled by the setClickCountToStart() method.

So ideally this will happen when the cell loses focus after editing

I agree with the other suggestion that you should probably be adding the TableModelListener to the TableModel. Although depending on your requirement you may want to consider using the Table Cell Listener.

camickr
I actually like the TableCellListener example that you have put up at the link. But my environment is Java 1.5 so in effect I cannot use the utility since "convertRowIndexToModel" is not available in 1.5. Is there a workaround?
Chantz
Also while we are at it, Is there a way to disable this behavior for certain columns?
Chantz
+1  A: 

i achieved that type of behaviour by overriding stopCellEditing ( i use a custom implementation of AbstractCellEditor )

public boolean stopCellEditing()
{
String s = (String) getCellEditorValue();
if ( ! valueValidator.isValid(s) )
  {
  editorComponent.setBorder(new LineBorder(Color.red));        
  Toolkit.getDefaultToolkit().beep();
  return false;
  }
}
else { ........
Peter