Access Keys:
Skip to content (Access Key - 0)

Cette page présente une série de trucs et astuces relatifs au composant JTable.

Preventing the current cell move down after pressing enter

Terminate edit on focus lost

The client property "terminateEditOnFocusLost" defines what to do when the focus goes outside of a JTable. This property is of type Boolean.
If this property is set, then it tries to commit the changes in the editor. If it can't be committed then the changes are cancelled.
If the value of this property is null, the default value is Boolean.FALSE. So, this feature is disabled by default in a JTable. To enable this feature, you have to set this property on your JTable such like this :

Preventing editing of cell when a key is typed

The client property "JTable.autoStartsEdit" defines what to do when a key is pressed when the cursor is on a JTable cell. This property is of type Boolean.
If the property is set to Boolean.TRUE, then the cell will start the edit mode when a key is pressed. If the value of this property is null, the default value is Boolean.TRUE. So, this feature is enabled by default in a JTable. To disable this feature, you have to set this property on your JTable such like this :

Grayout a non editable cell

If you don't want to change every TableCellRenderer of the JTable or if you simply can't do it, the easy way to grayout a non editable JTable cell is the following way :

  1. Nov 02, 2009

    Anonymous says:

    Thank you very much for these tips! JTable has still different behavior than Exc...

    Thank you very much for these tips! JTable has still different behavior than Excel when JTable.autoStartsEdit is TRUE. JTable appends new typed chars at the end of existing string whereas Excel deletes cell contents and starts a new string. Do you know how it can be elegantly resolved ?
    Thanks! Mikhail

  2. Nov 02, 2009

    Olivier Catteau says:

    Hello, I think you will have to implement your own TableCellEditor such like thi...

    Hello,
    I think you will have to implement your own TableCellEditor such like this (I didn't try this code so perhaps it doesn't work...) :

    I hope it helps ! If you have the solution, post a response.
    Thanks. See you !

  3. Nov 08, 2009

    Anonymous says:

    Thank you for the example! It was a good starting point for me. I have slightly ...

    Thank you for the example! It was a good starting point for me. I have slightly expanded your solution by overwriting isCellEditable() like in http://www.exampledepot.com/egs/javax.swing.table/CustEditClick.html and setting component to either "" or value.toString() depending whether it was selected by mouse double click or simply starting typing inside a cell (I know it from isCellEditable()). Fortunately undo action by pressing ESC is handled by Swing automatically. Now my JTable works exactly like Excel does!

    Mikhail

Add Comment

Adaptavist Theme Builder (4.0.3) Atlassian Confluence 3.2.1_01, the Enterprise Wiki: Intranet software for documentation and knowledge management