views:

487

answers:

4

Hi I have an app that is written in Swing, awt. I want to prevent users from pasting values into the textfields. is there any way to do this without using action listeners?

A: 

you may be able to override the paste method in jtextcomponent.

A: 

Or you can:

  • defined your own PlainDocument, then
  • associate it to your JTextFiled, and
  • override in your PlainDocument the insertString() method

The insertString Method will only insert anything if the custom attribute 'enablePaste' that you will have defined in this custom PlainDocument class is true.

VonC
+2  A: 

The best way is to remove action associated with CTRL+V keystroke in components ActionMap.

Rastislav Komara
A: 

The simplest way it to say: textComponent.setEditable(false);

This disables cut & paste, but copy is still enabled.

  • Rob, PhD
Rob