Table Select All Editor
Posted by Rob Camick on October 20, 2008
Sometimes when editing text fields it is desireable to have the text selected so that when you start typing the original text is replaced with the new text. This is easily done by adding a FocusListener to the text component and invoking the selectAll() method. Since a JTable uses a JTextField as an editor we should be able to add this text selecting feature to a table. However, it is a little more complicated then simply using a FocusListener.
A request to edit a cell can be made in one of three ways:
- by using a mouse click (usually a double click)
- by using the table edit Action which is invoked by using the F2 key
- by typing directly into a cell
Fortunately though all requests to edit a cell are handled by the editCellAt(…) method of the table. This method is also passed an EventObject which allows us to know which of the above methods was used to invoke the editor. So, by overriding the editCellAt(…) method we can access the editor and invoke the selectAll(…) method on the editor giving us the selected text in the editor. Therefore, I created the RXTable class which extends JTable and overrides this method. The RXTable class has 4 methods that allow you to control this functionality:
- setSelectAllForMouseEvent(…) – select the text on mouse events
- setSelectAllForActionEvent(…) – select the text when F2 is used
- setSelectAllForKeyEvent(…) – the first key event will invoke the editor and cause the text in the editor to be replaced with the typed key. The editor will now have focus so future key events will cause the text to be appended to the existing text in the editor.
- setSelectAllForEdit(…) – convenience method that invokes the above 3 methods.
The following line of code was used on the table shown in the image below which shows the table once the editor has been invoked:
table.setSelectAllForEdit(true);

jago said
RxTable uses the class TableDecoration which is missing.
Rob Camick said
Oops, that should be deleted. I just uploaded the proper version.
Thanks for the feedback, I’m surprised no one has mentioned it before, its been like that for a while.
jago said
Hmmm…I just downloaded the new version – still the same.
Rob Camick said
Works for me. Is it cached by your browser? Otherwise just delete the entire method where the errors are originating from.
jago said
I already deleted everything. I just wanted to let you know.
Still I cleared my cache and there is still a reference to TableDecoration:
private List decorations;
On a differen note – I would like to directly show the editor when moving into a cell. In your RXTable the editor is only shown when I type a key, like backspace. However I would like to directly see the editor with the text being selected when I move into a cell.
Is this possible?
Rob Camick said
Sorry, trying to do to many things at once. It should clean compile now.
> I would like to directly show the editor when moving into a cell
I already answered that question in your comment from the other posting.
Alex said
Hi Rob,
I’m getting some difficulties to implement your code in my jTable. Please, could you make an example?
(sorry by my english)
Alex
Rob Camick said
You can’t use your table. You need to use my class instead:
RXTable table = new RXTable(5, 3);
table.setValueAt("abcde", 0, 0);
table.setSelectAllForEdit(true);
Or you just use your TableModel:
RXTable table = new RXTable(anyTableModel);
table.setSelectAllForEdit(true);
If that doesn’t help then you need to email me your code by using the “Contact Us” page.
Alex said
Hi Rob,
It’s time to sleep in Spain!
I will try that tomorrow.
THANK YOU VERY MUCH!!
Alex
Tory said
why the new text do not replace the original text??
Rob Camick said
It works for me or I wouldn’t have posted the code. See my comment in reply 3 about sending me your SSCCE.
Tory said
RXTable table = new RXTable(xkrs.tabelNilai);
table.setSelectAllForEdit(true);
that are my code in netbean… xkrs.tabelNilai is my table model.
Rob Camick said
That is NOT an SSCCE so I can’t help.
Tory said
what is SSCCE?
eugene steiner said
I would like to speak to someone @ RxTABLE
My e-mail is: rxwiz2010@yahoo.com or
My Phone #: 928 284 8986
Gene
Rob Camick said
Ask a question in the comment or use the Contact Us page with your question.
Protuhj said
I made a modification to the selectAll(…) method, and seems to work better than your original code:
if (editor == null || !(editor instanceof JTextComponent)) {
return;
}
if (eo == null || eo instanceof KeyEvent) {
((JTextComponent) editor).selectAll();
} else {
editor.addFocusListener(new FocusAdapter() {
@Override
public void focusGained(FocusEvent e) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
((JTextComponent) editor).selectAll();
}
});
editor.removeFocusListener(this);
}
@Override
public void focusLost(FocusEvent e) {
editor.removeFocusListener(this);
}
});
}
Anonymous said
NetBeans warns that java.util.Vector has been made obsolete by the JDK1.2 collection classes, and should probably not be used. What should be used in its place?
Rob Camick said
The JDK API uses Vectors for JTable and the DefaultTableModel. If you want to create a custom TableModel you would probably use an ArrayList.
Joel said
It worked perfectly for me. Many thanks!!
Rob Camick said
Good to hear. Thanks for replying.
Mubashir said
Great Thanks your solution works perfect for me but i want to show cursor after edit, how can it possible? It requires Clicking after making edit to show Cursor.
Rob Camick said
After you finish editing a cell the renderer is displayed. A renderer is not a real component and the default renderer for a table is a JLabel which does not have a cursor. You could try to change the renderer to use a JTextField but then you may have problems getting the caret to show because a JTextField only paints the caret when it has focus. Since a renderer is not a real component it will never have focus.
Maybe another approach is to always place the cell in editing mode. The code for this would be something like:
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(); } } }