Most users have learned from working with Excel that they can start editing cell content by typing over it. JTable, however, inserts the typed characters, and does not overwrite the current content if the user just start typing. So, select the content of JTextField editors in JTable to make your application more user-friendly.
Here is a code snippet that will do the job1).
public class XCellEditor extends DefaultCellEditor { public XCellEditor() { super(new JTextField()); } public Component getTableCellEditorComponent(JTable parm1, Object parm2, boolean parm3, int parm4, int parm5) { Component c=super.getTableCellEditorComponent( parm1, parm2, parm3, parm4, parm5); if (c instanceof javax.swing.JTextField) ((javax.swing.JTextField) c).selectAll(); return c; } }
Install the editor with this line:
table.setDefaultEditor(java.lang.Object.class,new XCellEditor());