I have this problem: added a jcheckbox in a jtable. In my model this jcheckbox is loaded with getValueAt and used the method setValueAt when selected by the user.
No problem until I push the confirm button, when all my row selected (true value) disappear and the loop doesn't read anything with true value.
Where is my mistake? How can I update the model before pressing my confirmation button?
Sounds like you are looking for:
table.putClientProperty("terminateOnFocusLost", true);
This tells the table to try to commit any ongoing edits when the focus is transfered to the outside, that is to a component which is not a child of the table.
Related
I have a JTable (master) to which I added a ListSelectionListener to catch when a row is selected and populate another JTable (detail) according the selected row.
Now some requirements have changed and if come conditions are met, when the user clicks into another row, I should show a JOptionPane with YES/NO options, and only if YES is clicked then the new row can be selected.
How can I achieve this? Shall I use always ListSelectionListener? I don't think so as it is raised only after the selection is done.
yes, for sure
table.getSelectionModel().addListSelectionListener(new ListSelectionListener(){
public void valueChanged(ListSelectionEvent event) {
// do some actions here, for example
// print first column value from selected row
System.out.println(table.getValueAt(table.getSelectedRow(), 0).toString());
}
});
when the user clicks into another row,
What about when the user used the up/down arrow keys to move to another row?
The default behaviour in both cases is to select the row automatically.
You might be able to override the changeSelection(...) method of the JTable. I believe this is the method that is invoked by the mouse or keyboard logic to change row selection.
So you would add your logic to display the option pane and then if "Yes" is selected you would invoke super.changeSelection(...).
However, I would find it frustrating to try and use the keyboard to scroll down the table if this option pane keeps popping up. Remember to design a user interface that support both mouse and keyboard effectively.
I want to implement a JButton ("Delete row") to delete the row currently selected in a JTable.
JTable.getSelectedRow() works within the selection listener, but does not work out of it.
When the user presses the "Delete row", how can I figure out which row has been selected?
PD: So far, my idea is to create a variable, automatically updated each time the selection listener is called, and when the user presses the button check the value of this variable. However, it does not look very nice...
While going through my ListSelectionListener inner class, I found the next line
m_view.getInputsTable().clearSelection();
Comment the line, and keep working :D
I have a JTable with a several columns, one of which is has a custom renderer to display 3 buttons in a JPanel as well as a custom editor to allow them to be clickable. One of the buttons sends a delete command to our server for that row id then reloads the table data from the server by clearing the data model and loading data again. When this happens the cell with the 3 buttons continues to display (but not the rest of the row) until I click another button in another row even though the row is gone.
I've set putClientProperty("terminateEditOnFocusLost", Boolean.TRUE) but changing focus to another component does not help.
I've tried deleting the row itself before refreshing the data (even deleted all the rows) and made sure to call fireTableRowsDeleted().
I've also tried calling the cancelCellEditing() and stopCellEditing() functions of the TableCellEditor and even manually setting the editing row/column to another cell.
Any help would be greatly appreciated.
Ok, so I figured it out. I ended up calling removeEditor() on the table and that fixed it.
Thanks for the responses.
I've set putClientProperty("terminateEditOnFocusLost", Boolean.TRUE) but changing focus to another component does not help.
That method should be invoked when you create the table, not in the actionPerformed method when you click the button.
Ok, so I figured it out. I ended up calling removeEditor() on the table just before the data refresh and that fixed it.
Thanks for the responses.
i have a jtable with multiple editable cells
when someone presses the "save button" on the menu, it doesn't save the last value that is still currently being edited. (unless they tab out of the cell first)
i can check it is being edited by calling .isEditing() which returns true.
What i would prefer to do is trigger that the cell editing is complete, and it can show any validation errors, and if none, then save. (without the user having to tab out first)
can someone please point me in the right direction
thanks
You can manually call:
JTable table;
table.getCellEditor().stopCellEditing();
This should cause the table to stop editing at that cell. By overriding the stopCellEditing() method of your TableCellEditor and returning false under certain circumstances, you can indicate that the cell's value is currently invalid and thus editing cannot be stopped at that time.
when you have a jtable with multiple editable cells
when someone presses the "save button" on the menu, it doesn't save the last value then please add the below peace of code
The first is to have the table automatically stop editing and save the data. This can be done by setting a property on the table:
JTable table = new JTable(...);
table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
It will make sure that when someone presses the any button on the menu, it saves the last edited value also.
If I remember correctly, edited/added value is present in cell editor component by default it is EditBox, BUT do not present in table model. If it is possible try to check cell editor component it should help you.
I have a JTable bound to a List property. I used NetBeans to add a property to my jpanel form, enabled/checked propertyChangeSupport, and bound my jTable to that property. Table is displaying the data perfectly. Please guide me how can I enable/disable a 'Save' button when data of this table is changed by double-clicking a cell and editing its contents.
The Table Cell Listener will listen for real changes in the data of a cell.
A TableModelListener fires an event even if you tab out of the cell and don't actually change the data.
Have you tried adding a TableModelListener to the JTable's model? This should fire any time the table's data is changed, and the listener can then enable your save button. The button should disable itself whenever it is pressed and the data has been successfully saved.
Edit:
Please ignore this and instead go with camickr's recommendation!