Problem with Cell Renderer

I have hit a bit of a brick wall with this one -

I have defined a number of Cell Editors which block certain characters from being entered, this works perfectly well in the case of doubleclicking on the cell, but if you TAB to the cell and start typing, the KeyListener on the underlying TextField is ignored completely.

Essentially, although you can type, the JTextField component of the CellEditor does not actually receive focus - hence no KeyEvents are actually received.

I need to block such input! I thought of possibly enabling a GlassPane - but that seems cumbersome.

Any ideas?

[618 byte] By [dalemana] at [2007-11-27 5:25:21]
# 1

I can't imagine why it would block the key listener. Focus is focus, regardless of how it's achieved.

Otherwise I don't see how the chars get into the field, unless the editor is somehow redirecting the input.

A better solution is probably to create a custom document in your editor text field, so that you can filter out what you want there.

bsampieria at 2007-7-12 14:45:23 > top of Java-index,Desktop,Core GUI APIs...
# 2

Maybe you can use a JFormattedTextField as the editor component.

> A better solution is probably to create a custom document in your editor text field, so that you can filter out what you want there.

The newer approach is to use a [url http://java.sun.com/docs/books/tutorial/uiswing/components/generaltext.html#filter]Document Filter[/url].

camickra at 2007-7-12 14:45:23 > top of Java-index,Desktop,Core GUI APIs...
# 3

Thanks for the ideas guys, all good, but the problem is that the editing component (JTextField, JFormattedTextField or whatever) is given focus (ie focusGained() is called) if you double click on the cell, but if you highlight the cell, and then type a key, the character typed appears in the cell, although there is no caret, and the editor component receives no focus event - hence you cannot consume the event.

I have hacked a work-around by subclassing JTable and overriding processKeyBindings(), forcing the Component to focus. There were problems, as this method calls a protected method of JComponent - I have coded a workaround which works in my case but is far from a general solution.

I will post some code shortly.

dalemana at 2007-7-12 14:45:23 > top of Java-index,Desktop,Core GUI APIs...
# 4

> but if you highlight the cell, and then type a key, the character typed

> appears in the cell, although there is no caret, and the editor

> component receives no focus event - hence you cannot consume the event.

Thats why you use a DocumentFilter. You control which characters actually get inserted into the Document.

And thats why I believe (although I haven't test) that a JFormattedTextField should work as well, since it beeps at you when you try to insert an invalid character, which effectively consumes the event.

camickra at 2007-7-12 14:45:23 > top of Java-index,Desktop,Core GUI APIs...
# 5
Ah very good point re DocumentFilter - I will give that a whirl.
dalemana at 2007-7-12 14:45:23 > top of Java-index,Desktop,Core GUI APIs...