views:

92

answers:

2

How can I get JTextFields to allow Ctrl-Delete and Ctrl-Backspace when editing text?

In various other programs, these key combinations can delete an entire word in one go.

From what I can tell, the default behaviour of a JTextField allows the user to use CTRL to jump over an entire word when using left and right keys, and to select an entire word when SHIFT is used too. However, deleting just doesn't work.

A: 

You need to define an Action and put it into the action map of the composite. See this article for an introduction.

Aaron Digulla
+1  A: 

Swing uses Key Bindings to map Actions to components. To find out the default mappings for a given component you can use the Key Bindings program. The article also contains a link to the Swing tutorial which contains a section on "How to Use Key Bindings".

To create your custom Action you would extend TextAction so you have access to the text component. You would then need to get the current caret position. Then you can use the Utilities class to get the start or end of the current word and then you can remove the characters from the Document.

camickr