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
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:
@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);
// Will want to add an instanceof check as well as a check on Character.isLetterOrDigit(char).
char keypressed = ((StickyKeypressTable)table).getLastKeyPressed();
field.setText(String.valueOf(keypressed));
container.add(new JButton("..."), BorderLayout.EAST);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
// This needs to be in an invokeLater() to work properly
field.requestFocusInWindow();
}
});
return container;
}
As far as nastiness goes this sits somewhere up there with Vogon Poetry, but it should solve your immediate problem.