Java Tips Weblog

  • Blog Stats

    • 2,065,822 hits
  • Categories

  • Archives

  • Advertisements

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:


Get The Code

See Also

Table Select All Renderer

Related Reading

Java API: javax.swing.table.editCellAt(…)


26 Responses to “Table Select All Editor”

  1. jago said

    RxTable uses the class TableDecoration which is missing.

  2. 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.

  3. 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)


    • 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);

      Or you just use your TableModel:

      RXTable table = new RXTable(anyTableModel);

      If that doesn’t help then you need to email me your code by using the “Contact Us” page.

  4. Alex said

    Hi Rob,

    It’s time to sleep in Spain!

    I will try that tomorrow.



  5. Tory said

    why the new text do not replace the original text??

  6. I would like to speak to someone @ RxTABLE

    My e-mail is: or

    My Phone #: 928 284 8986


  7. 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)) {
    if (eo == null || eo instanceof KeyEvent) {
    ((JTextComponent) editor).selectAll();
    } else {
    editor.addFocusListener(new FocusAdapter() {
    public void focusGained(FocusEvent e) {
    SwingUtilities.invokeLater(new Runnable() {
    public void run() {
    ((JTextComponent) editor).selectAll();

    public void focusLost(FocusEvent e) {

  8. 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?

  9. Joel said

    It worked perfectly for me. Many thanks!!

  10. 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();

  11. How can I use this If I use netbeans Jswing for creating my table

    • Rob Camick said

      I don’t use Netbeans so I can’t tell you how to use the IDE. My suggestion is to not use an IDE. Instead create your forms manually, then the code will be portable if you ever switch IDE’s. Just use the IDE to help compile and debug, but don’t use it to generate your code.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: