Cell editor not exiting when row is deleted - java

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.

Related

Jtable losing multiple row selection,when button in one of the column is clicked

I have a very basic problem with jtable. I have a jtable that has multiple columns with one of column having a button. When i click on that button a panel drops, and asks to select an option from given options. When i select that option, value replaces in one of the column.
Now, i want when i select multiple rows, and do the same thing as above, it should replace that column in all the selected rows.
Problem: Currently, my table is losing selection when i am clicking the button in one of the column in jtable after multiple row selection.
I searched google and stackoverflow a lot, but could not find anything meaningful. Anyhelp or sample code is appreciated.
Thanks
If I understood your problem correctly then the solution is fairly simple.
First of all the issue probably occurs because once you click on the button java sets a new focus on the button and therefore clearing the focus on the other rows. That won´t be a problem in a single selection because you still click into the selected row, however doing that with multiple rows in one go won´t work that way.
To solve this you need to save your previous selections in something like an ArrayList and after the whole option thing you can apply the changes to every element in the ArrayList and reload the table.
A cleaner and more intuitive approach though would be to place the button outside of the JTable.

TableModel.fireTableCellUpdated(tableRowIndex, tableColumnIndex) is not updating the cell in JTable

I have a JTable in my code.
And whenever there is an update to any specific column in the row (cell basically), I will update the corresponding icon in that cell.
so I'm basically following these steps.
Step 1: I update the model.
Step2 2: I'm calling
tableModel.fireTableCellUpdated(tableRowIndex, tableColumnIndex);
This works fine.
But problem comes when I drag and drop the columns from one position to another in Table header. And whenever there is an update to any specific cell, I follow the same steps as I mentioned before.
Problem: I'm not seeing the Icon painted. But if I bring the focus on top of that row in table it is painting the icon.
Observation: I see the tableRowIndex and tableColumnIndex are correct after dragging the columns.
Just for testing I added this piece of code in the problem scenario.
examTable.repaint(examTable.getCellRect(examTableRowIndex, examTableColumnIndex, true));
This is repainting the cell properly.
But this is not the right solution I guess. I tried to debug the code I didn't find much about the problem
I'm calling tableModel.fireTableCellUpdated(tableRowIndex, tableColumnIndex);
That is wrong. You should never invoke the firXXX methods directly. That is the job of the TableModel to invoke the appropriate event when the data is changed.
I will update the corresponding icon in that cell.
All you need to do is invoke model.setValueAt(...) method to change the Icon and the model will notify the table that data has changed so the table can repaint itself.
examTable.repaint(...)
Again you should not need to manually invoke repaint on the table
But problem comes when I drag and drop the columns from one position to another in Table header.
Not sure why you need special code for this if you follow the advice from above. But if for some reason it is still necessary then you need to look at the convertColumnIndex...(...) methods to make sure you are using the proper index for your column.

Changing current selection in JXTable doesn't work?

I have a JXTable where the users need to introduce data, then save it. Only the thing is, the user has to deselect the last edited cell before saving it. If they don't, the data of that cell isn't saved.
The only thing I thought of is to change the current selection automatically just before saving. This is what i tried :
table.getSelectionModel().setSelectionInterval(0, 0);
table.getColumnModel().getSelectionModel().setSelectionInterval(0, 0);
OR
table.getSelectionModel().setLeadSelectionIndex(0);
table.getColumnModel().getSelectionModel().setLeadSelectionIndex(0);
None of both seem to work yet these are the only two methods I found to do this.
Can anyone please tell me how to do this properly or propose an alternative to also let it save the data from that last cell?
I am assuming the user clicks on another component (JButton) when he wishes to save data. If you have a reference to the JXTable when that event happens you could add the following piece of code there:
if (table.isEditing()) {
table.getCellEditor().stopCellEditing();
}
The stopCellEditing() should save the state of the model and allow you to save all the contents, including the currently selected / edited cell.
EDIT: As kleopatra pointed out, the default (and better!) way to handle this is through the client property of JTable component:
table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
For JXTable this should already be set though, which indicates that the way your UI handling of the save functionality works does not include moving the focus away from the table. So in essence you'd be better off changing the focus when your 'save' event is being fired.

JTable erratic behavior

I have some old Java application that uses JTable to show some data and allow input as well.
JTable has 2 columns. Next to JTable, there is button that adds new row into JTable.
My problem is behavior of Tab and Enter keyboard keys and mouse clicks when you navigate JTable.
I will try to explain:
Let say there are 5 rows in JTable:
if I click with mouse on one row, sometime whole row gets highlighted, and sometime cell gets into edit mode (in addition to whole row being higlighted)
Sometime, selected row gets highlighted, but cell above or below highlighted row gets into edit mode
If I use Tab to skip from one field to another, editable field is always above highlighted row.
There are other issues as well.
Any ideas what might be wrong with it?
That sounds a bit odd. Are you fully invalidating your table and notifying listeners etc that the number of rows have changed?
In your table model, loop over all of your TableModelListeners and fire a TableModelEvent 'insert' event:
TableModelEvent event = new TableModelEvent(
modelInstance,
positionOfNewRow,
positionOfNewRow,
TableModelEvent.ALL_COLUMNS,
TableModelEvent.INSERT);
for (TableModelListener l: listeners) {
l.tableChanged(event);
}
If you can't work out the problem, it is possible to write your own behaviour for the table.
These are some useful methods:
editCellAt(int row, int column);
rowAtPoint(Point point);
columnAtPoint(Point point);
You can add your own listeners to the table to intercept events and edit cells in any way you like.
Turn off the Table cell editing. And see how it behaves. Typically this can be handled by changing the TableModel.isCellEditable() method to simply return false.
Turn it back on. Is your instance of JTable subclassed? Is it overriding editCell()? If so that's the method that will trigger an edit based on the event occurring or not. That method is turning on editing inappropriately. The implementation of that method is bad if it's overridden.
If your table isn't subclassed look for calls to editCell(). Those are probably calling it inappropriately so you can look for those calls and start setting breakpoints or log statements.

java swing jtable screen location of cell renderer component

I'm new to swing and I faced my first serious problem, here goes:
I have a JPanel with JTable and lots of checkboxes below the table. I'm trying to align the checkboxes below the table with checkbox located in the first table column. The problem is - this has to be done when model data changes.
Immediatly after the fireTableDataChanged() gets triggered in JTable there is no way to get the screen location of its custom cell renderer component (which in my case is JCheckBox).
As far as I understand this happens because JTable gets redrawn asynchronously. Whenever I try to get the location the IllegalComponentStateException gets thrown.
Any ideas on this are highly appreciated.
You shouldn't do it manually. You should use the appropriate layoutManager. Check this link for the different Layouts:
http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
This might help you to get screen location of row and column.
Rectangle rect= cartTable.getCellRect(row,column,true);
//cartTable-Jtable object name
int x=Double.valueOf(rect.getX()).intValue();
int y=Double.valueOf(rect.getY()).intValue();

Categories

Resources