How to call a method after the space bar is pressed in a JTextArea

java, jtextarea, swing

Solution

Read the Swing tutorial on How to Use Key Bindings.

The tutorial has examples and you can find plenty of other examples in the forum.

When you create the custom Action that you want to execute you would extend TextAction.

Problem

Sorry for what is likely a simple question, but how can I call a method every time the space bar is pressed in a JTextArea? I have tried attaching a keylistener to the text area, but I wasn't able to get that to work. Thanks.

Original source