问题
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?
回答1:
You can just call setTransferHandler with a null parameter like this:
textComponent.setTransferHandler(null);
This will disable all copy/paste actions on the field.
回答2:
The best way is to remove action associated with CTRL+V keystroke in components ActionMap.
回答3:
The simplest way it to say: textComponent.setEditable(false);
This disables cut & paste, but copy is still enabled.
回答4:
public class PastlessJTextField extends JTextField {
public PastlessJTextField() {
super();
}
public PastlessJTextField( int columns ){
super( columns );
}
@Override
public void paste() {
// do nothing
}
}
回答5:
You may be able to override the paste()
method in JTextComponent
.
来源:https://stackoverflow.com/questions/316673/disabling-paste-in-a-jtextfield