Search code examples
javaswingkeypressjtextarea

How to stop key repeat on continuous keypress?


For example if I pressed the key "1" for few seconds the result in my JTextArea will be "1111111111111".

Is there any way to stop it after one char?


Solution

  • If I understand the problem correctly, you could use a DocumentFilter for this, and only allow the text to be inserted if let's say the delay of the keypress is above 0.5 seconds.

    Example:

    import javax.swing.JFrame;
    import javax.swing.JTextArea;
    import javax.swing.text.AbstractDocument;
    import javax.swing.text.AttributeSet;
    import javax.swing.text.BadLocationException;
    import javax.swing.text.DocumentFilter;
    
    public class Example {
    
        public Example() {
            JTextArea textArea = new JTextArea();
            ((AbstractDocument) textArea.getDocument()).setDocumentFilter(new DocumentFilter() {
                String lastStr = "";
                long time = System.currentTimeMillis();
    
                @Override
                public void replace(FilterBypass fb, int offset, int length, String str, AttributeSet attr)
                        throws BadLocationException {
                    long delay = System.currentTimeMillis() - time;
                    time = System.currentTimeMillis();
                    if (str.equals(lastStr) && delay <= 500) {
                        return;
                    }
                    lastStr = str;
                    super.replace(fb, offset, length, str, attr);
                }
            });
    
            JFrame frame = new JFrame();
            frame.setContentPane(textArea);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(800, 600);
            frame.setLocationByPlatform(true);
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            new Example();
        }
    }