Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a Jtable save data whenever a cell loses focus?

Tags:

java

swing

jtable

The high level: I have a JTable that the user can use to edit data.

Whenever the user presses Enter or Tab to finish editing, the data is saved (I'm asusming that "saved" really means "the TableModel's setValueAt() method is called".)

If the user leaves the cell in any other way after making an edit, the new data is not saved and the value stays the way it was. So, for example, if the user changes a value and then clicks on some other widget on the screen, the change doesn't "stick."

I believe that this is the default behavior for a JTable full of Strings, yes?

For a variety of reasons, the desired behavior is for the cell to save any and all edits whenever the user leaves the cell. What's the best/right way to get Swing to do this?

like image 868
Electrons_Ahoy Avatar asked Oct 31 '09 00:10

Electrons_Ahoy


People also ask

What is JTable and its purpose?

The JTable class is a part of Java Swing Package and is generally used to display or edit two-dimensional data that is having both rows and columns. It is similar to a spreadsheet. This arranges data in a tabular form. Constructors in JTable: JTable(): A table is created with empty cells.

How do you make a JTable cell editable?

jTableAssignments = new javax. swing. JTable() { public boolean isCellEditable(int rowIndex, int colIndex) { return editable; }};

How do you make a JTable column not editable?

Right-click on the table cells. From popup menu, choose "Table Contents..". Uncheck the editable check box for the column you want to make it non-editable.

How can I tell if a row is selected in JTable?

So you can call table. getSelectionModel(). isSelectionEmpty() to find out if any row is selected.


1 Answers

Table Stop Editing explains whats happening and gives a couple simple solutions.

like image 125
camickr Avatar answered Oct 04 '22 11:10

camickr