Java Tips Weblog

  • Blog Stats

    • 1,759,481 hits
  • Categories

  • Archives

Table Editing

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:

table.putClientProperty("JTable.autoStartsEdit", Boolean.FALSE);

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.

See Also

Table Select All Editor

6 Responses to “Table Editing”

  1. jago said


    nice post, but I would prefer a different behavior. I would like to keep the autoStartEdit behaviour BUT:

    it should act more like Excel. If I move to a new cell and press any button, except the buttons to move to another cell (tab, return, arrow buttons, etc.) it should delete the old content of the cell and add whatever I type.

    Any idea how to do that?

    • Rob Camick said

      I updated the blog to include the relevant link in the “See Also” section.

      • jago said


        Playing with it for a while I wonder if it wouldn’t be more convenient to directly activate the editor and select all text once the user moves to a new cell. At this point has the following options:

        1. Leave the cell: Pressing one of the ‘leave cell’ buttons: tab, arrow buttons, enter, etc. should move away from the cell changing nothing.

        2. Edit cell content: either just typing away and deleting everything OR press “Home” or “End” or the left move button (arrow buttons should leave the cell) to deselect text and move to a special position within the text.

        The whole point of this behavior would be to allow the user to just use the keyboard to QUICKLY edit the cells but without loosing all of the data within the cells (which somehow doesn’t sound quick anymore).

        Do you think this is possible in a user-friendly and non-confusing way?

        Also I have no clue how to do it – perhaps another post for your great website :)

      • Rob Camick said

        The solution I gave you mimics the default behaviour of my version of Excel, which is what you originally asked about.

        You are now asking for different behaviour, so of course it will require a different solution.

        I suggest you start using the Sun forums. I just happened to answer this question last night:

      • jago said

        Sorry. Normally I am informed about followup comments by email. Didn’t get it in this case.


  2. annie said

    Thank you, I’ve just been looking for info about this topic for a while and yours is the best I’ve came upon so far. However, what about the conclusion? Are you positive about the source?

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: