I have an editable JTable. If a user is editing a cell when the underlying row model is updated the cell editor loses focus.
What's the simplest solution to this?
The simplest solution to this, as #mKorbel noticed, very theoretical question :) is to:
Always remember which cell has the focus.
Listen to row changes.
When the underlying row is changed at the end of the method, called after the change is done, give back the focus to the right cell, e.g. using requestFocus(), example available here.
Related
I need to customise the selection behaviour of the cells inside a JTable descendant. I have a custom cell editor using a JTextField descendant as the editorComponent; there's a focus listener registered on it which manages the desired selection behaviour.
I need different behaviour when tabbing around the table than when another window or application comes to the front and then goes away again.
This is currently not possible, because the cell editor's editorComponent seems to have no off-the-shelf way of knowing (or telling it) that it's the editor for a table cell, so it doesn't know that it's "inside" the table, so my focusGained() and focusLost() think focus is moving between different windows even if I'm just tabbing around in the table.
SwingUtilities.windowForComponent() returns null for the editorComponent.
Before I roll my own solution, is there an accepted way of dealing with this issue? I can't be the first person to need to do this...
I'm writing an application that has a JTable, and an edit button that sets the current selected row to be editable. Then once the user is done altering the data, they can click the edit button again (with text that now says "Save") to save the data.
The problem is though, when I set a row to be editable, there isn't a visible difference. I could add some code to the renderer to draw the editable cells a little differently, but I don't know what the proper way to make a cell look editable is. Change the color? Make it look like a JTextField? What's the standard method?
Thanks!
Really this is a user interface design question, not a programming one.
To do what you want you need to supply an appropriate cell renderer with the changes you desire but you will need to decide on your own settings. One option might just be to look at the difference between an editable and non-editable text area and apply those to all the cells on the table. This may be as simple as setting the renderers to disabled for any read-only rows.
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.
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.
This Scenario may sound silly... but this was the observation...
When a mouse click on the the JTable cell...
Cell is gets into the editor mode, while in the editor and a invalid entry is made (JTextFeild Component is installed in each cell), the focus is restricted in the editor mode by returning false instead of super.return stopCellEditing(); while validation and test field is painted red.
In the false mode, if user clicks anywhere on the table or outside the table the focus is not lost, but when the user clicks on the JTable header the focus is lost from the cell... I need to restrict this... How can this be achieved
Thank You in Advance...
just to make sure: the editing is canceled in that case (though not exactly on click but on any mouse gesture which might be interpreted as starting a column move/resize, if I remember correctly), right?
If so, it's
a long standing bug, table silently removes editors on receiving change notifications from the column model
even if fixed, (arguably, of course) not the best user experience to not allow moving out
You might look at #camickr's TablePopupEditor, which uses a modal dialog to edit.
This can actually be possible by disabling
table.getTableHeader().setReorderingAllowed(false);
table.getTableHeader().setResizingAllowed(false);
Currently its working, but don't know under what circumstances it may fail.
Thank You