Search code examples
javaswingcopyclipboardjtextarea

Best way to prevent Ctrl+c on JTextArea


I want to prevent copying the text from the JTextArea. What is the best approach to do that? I found a KeyListner solution but it didn't seemed best. I don't want to use a key listner for that. Is there a shortcut method/way that I can use?


Solution

  • Not tested, but I would simply override copy() and cut():

    @Override
    public void copy() {
        // does nothing
    }
    
    @Override
    public void cut() {
        // does nothing
    }
    

    Looking at the source code, it should work, since that's what JPasswordField does to prevent cutting/copying.