Search code examples
javaswingjtabletablecelleditor

Select all data when start editing JTable Cell


I'm trying to make a table that works like Excel. Meaning, when a user starts to insert data into the cells the content into them is selected and changed by the new data inserted.


Solution

  • You can create a custom TableCellEditor for your table. This class will have an instance variable of a TextField, lets call it textField. Then the getTableCellEditorComponent method could look like this:

    public Component getTableCellEditorComponent(JTable table, Object value, 
                                 boolean isSelected, int row, int column ) {
        textField.setText(value.toString());
        textField.selectAll();
        return textField;
    }