JTable Header Focus Query? - java

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

Related

Getting the selectionLayer.getSelectedCells() as empty but not always

When I select a cell and start typing any value it edits the cell.
But when I am sorting few columns(2 to 3) by default using this code below,
natTable.doCommand(new SortColumnCommand(sortHeaderLayer, i, true, sortColumn.getSortDirection()));
and then select cell and start typing for first time or maybe first few times it edits then it won't.
When I debug the code I found that I was getting the selected Cells as empty after few successful editing, and that happens randomly like sometimes after 1 or 2 or 4 .. successful editing, then the selected Cell becomes empty. The cell is selected and is clearly visible but still we get empty in the code.
ActivateEditorAction.run()
The above code is used for the Action for the cell editing on selection. In the else block of the synchronized block when we are checking for isSelectionAndTypedKeyValidForEditing(), there the selected cells is coming as empty, not always but once it starts coming as empty then it keeps on coming as empty all the time even though selection is visible on the table.
Code for the function isSelectionAndTypedKeyValidForEditing()
There are certain events that cause a selection clearing. By default sorting is causing such an event.
If you are using GlazedLists you should also be aware that there are multiple events that are fired in case of sorting. The GlazedListsEventLayer tries to squash those events to a single event, but for huge lists it can happen that still multiple events are fired if the time frame in which GlazedLists is firing events is longer than the squashing time.
Maybe you are hitting a concurrency issue somewhere. There is not enough information about your NatTable setup and too much custom code that overrides the default configuration.
Apart from this information it seems to be a quite specific issue that depends on your configuration and your custom code. And that seems to be much more complicated than to be solved easily via Stackoverflow. IMHO you need professional support in this area. Some project members offer sponsored support in that area.

Need to "link" table cell editor with its table

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...

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.

Force JTable to "commit" data to model while it is still in editing mode

I have a JTable as follow.
So, while the JTable is still in editing mode (There is a keyboard cursor blinking at Dividend column), clicking OK directly will not commit the data into table model. Clicking OK merely close the dialog box.
I need to press ENTER explicitly, in order to commit the data into table model.
While JTable is still in editing mode, before closing dialog box, is there any way I can tell the JTable by saying, "Hey, is time for you to commit the changes into your model"
The source code for this dialog box is as follow Dialog Box Source Code. Do look at jButton1ActionPerformed for the executed code when OK is pressed.
I'm not sure if it will work (it would have been nice to have a SCCE), but try this:
TableCellEditor editor = table.getCellEditor();
if (editor != null) {
editor.stopCellEditing();
}
Table Stop Editing gives a couple of approaches.
EDIT
Example from article:
table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
Example from article:
if (table.isEditing())
table.getCellEditor().stopCellEditing();
To make the whole stable stop editing completely in any state (editing or not), you can call editing stopped:
table.editingStopped(new ChangeEvent(table));
That way you don't have to check for editors/state/etc.

How to stop editing with DefaultCellEditor when a separate JBtton is pressed

I got a table with a custom TableCellEditor (extending DefaultCellEditor) with a JFormattedTextField as the editor component.
Now I got problem: when I press a separate button while editing. When the button is pressed, the editor remains "open and active" while I'd want it to stop editing so that the changes made would be available for the operations caused by the button.
So how to cause the editing to be stopped when a distinct button is pressed. I tried setFocusLostBehavior(JFormattedTextField.COMMIT) for the JFormattedTextField but it didn't seem to have effect.
Hope somebody has some good ideas =)
You may try
jtable.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
Thanks asalamon74, that works quite nicely.
There's debate over the thing at Sun Bug Database : Bug 4724980: JTable: Add API to control what happens to edits when table loses focus. (also other bug entries are found). The terminateEditOnFocusLost turns on "commit-or-cancel" behaviour that can be seen at the Java sources at the class JTable$CellEditorRemover
And yes, it's somewhat strange that it isn't documented at the APIs etc...
Had also more issues with these things, see the question Java Swing : changing JTree selection while editingstopCellEditing() : how to get stopCellEditing() called before TreeListeners:valueChanged?

Categories

Resources