• Post Reply Bookmark Topic Watch Topic
  • New Topic

Intercepting control-X in a JTextField  RSS feed

 
dave taubler
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm developing an app where I've used Swing's undo framework to allow the user to undo/redo significant actions. There are various JTextFields in the app, and if the user uses the application's Edit -> Cut menu item (for example), an UndoableEdit is invoked, and the text is cut; the user can easily undo the cut afterwards. Standard stuff.

The problem is that Swing is a little too convenient... JTextFields have cut & paste built in. So if a user highlights text in a JTextField and types control-X, the text is cut. Great, but I can't capture that event and make it undoable! That makes the app a bit unpredictable and inconsistent.

The closest I've come is to add an UndoableEventListener to my JTextFields. In doing so, I receive notifications of the control-X actions, but I also get notification of every other darned undoable event as well. I can't find a way to determine that an UndoableEvent my listener received is a "cut" operation (or paste, for that matter, which should also be undoable). I was going to try some convoluted method to determine if control-X, or control-V, was just typed, but that sounds wrong (different keyboard mappings, some keyboards have "Cut" and "Paste" buttons, etc.)

So, any advice? It's got to be possible to do this, but I can't seem to find it...
 
susmit shukla
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Assuming that you have setup a menuitem which handles cut operation, you can define the keyboard accelerator which will result in calling the menuitem's listener (similar to user selecting the menuitem)

sample code:
menuItem_cut.setAccelerator( KeyStroke.getKeyStroke( KeyEvent.VK_X, InputEvent.CTRL_MASK ) );
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!