Posted by Rob Camick on December 26, 2008
There are 3 different ways to edit a cell containing text in a JTable:
- use F2 to invoke the editor
- use a mouse double click to invoke the editor
- just start typing on the cell with focus.
In the first two cases the editor actually gains focus and the caret is visible. This means that you can position the caret anywhere in the cell and change any part of the text. So you have full control over what you are editing.
In the last case, you have no control over where the text is added as the text you type is simply appended to the end of the existing text. This may or may not be the behaviour you desire.
In case this isn’t the behaviour you desire, then one option you have is to turn off default editing by typing directly into a cell. This is done using the following table property:
Now this will force the user to place the cell in editing mode first, by using either F2 or a mouse double click, before changing the data.
Like all client properties, this is an undocumented feature and could be removed from the JDK at any time. However these properties are generally added to support bug fixes and I doubt they will be removed unless a proper API method is added as a replacement.