Search code examples
javajtable

Excel-like JTable Cell Editor, with an empty cell at edition start


I'm trying to have a JTable edition working like in excel: if I start editing a cell, the cell becomes empty for receiving the new entry. The goal is to avoid the user having to delete the existing cell content first.

I can already detect cell start/stop edition by adding a PropertyChangeListener working as follow:

public void propertyChange(PropertyChangeEvent e)
{
    //  A cell has started/stopped editing
    if ("tableCellEditor".equals(e.getPropertyName()))
    {
        if (table.isEditing())
            processEditingStarted();
        else
            processEditingStopped();
    }
}

Trying to clear the table model at processEditingStarted() does not work with the code bellow:

private void processEditingStarted()
{
    SwingUtilities.invokeLater( this );
}

public void run()
{
    row = table.convertRowIndexToModel( table.getEditingRow() );
    column = table.convertColumnIndexToModel( table.getEditingColumn() );
    oldValue = table.getModel().getValueAt(row, column);
    newValue = null;

    table.getModel().setValueAt(null, row, column);
    table.updateUI();
}

However the editor content keeps the former value.

Does someone has clues to hide the current value of the cell during edition?

Optionnaly it should be able to restore the former cell content if the user did not actually modify the value.


Solution

  • I"ve never seen a spreadsheet remove data when you start editing a cell.

    Table Select All Editor shows a different approach.