Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make JTable cells default to overwrite mode

Tags:

java

jtable

JTable cells by default are in append mode when the cell is double clicked on. Is there a way to make the cell default to overwrite mode instead, IOW, select the contents of the cell so that if the user started typing the old text would be replaced by the new text without having to manually erase it?

like image 919
jacknad Avatar asked Dec 28 '22 05:12

jacknad


1 Answers

You should be able to do this by creating your own TableCellEditor, which can be assigned to the parent table using setCellEditor(). This object is a factory that is called by the JTable whenever the user starts editing a cell, to create the field used to do the actual editing. You can return your own JTextField, and simply avoid setting the old value to achieve what your asking. You'll also have to attach a listener to the text field to update the value in the table when the user has finished typing.

like image 72
Emil H Avatar answered Dec 30 '22 18:12

Emil H