What I want to do is change the cell (focus), the next selected cell will have all the selected text, ready for the user to completely change it.
The decision depends on your exact requirement. JTable has a visualization tool and editor.
The output usually shows the text in the cell. If you want the text to be replaced as you type, you need to do two things:
a) change the renderer to display the text in the โselectedโ state, so that the user knows that entering a character will delete the existing text b) change the editor to select all the text when it is called
This approach is relatively complex because you will need a separate renderer for each other data type in your table (i.e. String, Integer).
Or another approach is to automatically put each cell into edit mode when it focuses, and so you only need to change the editor to select the text.
This approach is simple as you can:
JTable table = new JTable(data, columnNames) { // Place cell in edit mode when it 'gains focus' public void changeSelection( int row, int column, boolean toggle, boolean extend) { super.changeSelection(row, column, toggle, extend); if (editCellAt(row, column)) { Component editor = getEditorComponent(); editor.requestFocusInWindow(); ((JTextComponent)editor).selectAll(); } } };
source share