Disabling 'paste' in a jTextfield

社会主义新天地 提交于 2019-12-03 16:19:42

You can just call setTransferHandler with a null parameter like this:

textComponent.setTransferHandler(null);

This will disable all copy/paste actions on the field.

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

Rob

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

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

public class PastlessJTextField extends JTextField {

        public PastlessJTextField() {
            super();
        }
        public PastlessJTextField( int columns ){
            super( columns );
        }

        @Override
        public void paste() {
            // do nothing
        }


    }

You may be able to override the paste() method in JTextComponent.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!