0

I have a JTextArea component in my Swing code and I would like to add a handler/listener that is triggered anytime the user is typing (key events; key up, key down, etc.) text in the JTextArea.

According to the JavaDocs above, I can add a DocumentListener to it's interna; Document model. However, when I implement DocumentListener, I have to write implementations for:

  • removeUpdate
  • insertUpdate
  • changedUpdate

These were not the type of methods I was expecting to see! I was expecting to see methods like onKeyDown(KeyEvent e), onKeyUp(KeyEvent e, etc.

So I ask: how can I get my JTextArea to respond to key up/down events? Thanks in advance!

IAmYourFaja
  • 55,468
  • 181
  • 466
  • 756
  • 1
    The question is, what do you want to achieve? If you want to filter the input, you should use a `DocumentFilter`, if you want to respond to certain key events (like tab or enter) you should use [key bindings](http://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html) – MadProgrammer Oct 16 '12 at 22:03
  • You never responded to our requests for clarification, and this may result in your following the wrong advice. – Hovercraft Full Of Eels Oct 17 '12 at 01:47

2 Answers2

3

This is may be a good situation for using Key Bindings. In a second I'll get you more information on this. In general you would try to avoid use of a KeyListener as this is considered a low level a construct and may be too low for this sort of thing, and often Key Bindings are preferred (as per the Key Bindings tutorial).


Edit 1
Here you go: How to use Key Bindings

Direct quote from the Swing Tutorial:

An alternative to key bindings is using key listeners. Key listeners have their place as a low-level interface to keyboard input, but for responding to individual keys key bindings are more appropriate and tend to result in more easily maintained code. Key listeners are also difficult if the key binding is to be active when the component doesn't have focus. Some of the advantages of key bindings are they're somewhat self documenting, take the containment hierarchy into account, encourage reusable chunks of code (Action objects), and allow actions to be easily removed, customized, or shared. Also, they make it easy to change the key to which an action is bound. Another advantage of Actions is that they have an enabled state which provides an easy way to disable the action without having to track which component it is attached to.


Edit 2
Note, there are times when you will want to filter the text entered into your component, and where you would use a DocumentFilter. I think that we are somewhat hampered by your current question since you've not yet told us what the overall goal of this functionality is. Please tell us more so that we can give you a better more complete and more correct answer.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
1

Use addKeyListener (it inherits from Component)

Matt
  • 43,482
  • 6
  • 101
  • 102
  • 2
    While technically not incorrect, without context, this may not be the correct solution for what the OP want's to achieve. If they want to filter the keys to prevent the user from entering certain characters, this is the wrong place to achieve it, if they want to perform an action on a given key stroke, they are better off using key bindings as some key strokes won't raise an event with the key listeners, also, pasting text into the field will not raise key events. IHMO – MadProgrammer Oct 16 '12 at 22:05