I have many custom editors for a JTable and it's an understatement to say that the usability, particularly in regard to editing with the keyboard, is lacking.
The main reason for this is that my editors are always created with a similar (though often more complex) situation to this:
@Override
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
JPanel container = new JPanel();
container.setLayout(new BorderLayout());
container.add(field, BorderLayout.CENTER);
field.setText((String) value);
container.add(new JButton("..."), BorderLayout.EAST);
return container;
}
I.E a panel with more than one component inside. The actual text editor is a descendant of the component being returned as the editor.
So, rendering issues aside, from what I can tell, the JTable is focusing the component that is returned by the getTableCellEditorComponent
method so when you press a key with a cell highlighted it passes focus and the key press to the panel, thinking that's the editor.
Is there anyway I can inform JTable that the "real" editor is the JTextfield?
Adding a hacky requestFocusInWindow
on the correct component is insufficient as the key press won't get passed on.
I think that I solved it.
To tell you the truth, I don't know what solved the problem, since I'm using a custom editor, a custom renderer and stuff...
When a cell is highlighted and I press "abc", the 3 letters go on screen (cell, in this case).
Well... I tried... =)
(I don't know if it's the same because my JTable uses JTextField and JComboBox as editors).
Check some related articles here and here.
Another good article about JTable editing in general.
I had very similar problem. In my case I had complex
TableCellEditor
which consists of JSpinner and some other components. The problem was that when cell editor started I wanted to transfer focus to its internal component. I fixed this by callingpanel.transferFocusDownCycle()
but this in turn caused keyboard events to stop working - when my internal component had focus and I pressed key up, I was expecting that component will intercept this event and change its value. Instead table changed row focus to one above... I fixed this by addingKeyListener
and dispatching all key events directly to the internal component.This is wrapper class based on
JPanel
I wrote to make my life easier.I fixed something similar in 2 steps
First override the editCellAt from your JTable and call requestFocus after preparing the editor:
Then overload the requestFocus from your JPanel and make sure your textfield is put as editorComponent:
You can always grab the keyEvent and set it yourself:
If I read your question correctly, you want the user to be able to type into a cell immediately, without activating the cell editor first, i.e., you want whatever keystroke activated the cell to be the first character entered into the text field.
My first attempt was to add a propertyChangeListener on the focusOwner property of the KeyboardFocusManager, only to notice that the focus never leaves the JTable. You probably ran into that as well. Time for plan B.
I got that "first keypress" thing to work by adding a KeyListener to the table that records the last KeyEvent for the keyPressed() method in an instance field. The getTableCellEditorComponent() method reads the character from there. I also needed that hacky requestFocusInWindow() call you mention if the user is to keep typing any characters after the first one.
For my sample app, I created a subclass of JTable that adds a KeyListener to itself. It's a much better idea to make your CellEditor instance implement KeyListener and add that to the regular JTable instead, but I'll leave that to you.
Here's your code snippet as I modified it:
As far as nastiness goes this sits somewhere up there with Vogon Poetry, but it should solve your immediate problem.