-1

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?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Hen Shabat
  • 519
  • 1
  • 6
  • 19
  • 3
    yes, but what if someone wants to hold the key down? You are going to break certain established UX expectations. Why do you need this approach? What problem are you trying to solve? – KevinO May 08 '16 at 17:32
  • I have a calculator and I don't need this function at all. – Hen Shabat May 08 '16 at 17:34
  • 3
    Let the user decide how to input data into the text area. Take for example this comment box. It makes no sense for me to hold down the 111111111111111, but the comment box allows it. Don't worry about something trivial like this. (1-). – camickr May 08 '16 at 17:37
  • 1
    Then are you *certain* that a `TextArea` is the appropriate way to collect input? For example, consider the `JTextArea` documentation [A JTextArea is a multi-line area that displays plain text.](https://docs.oracle.com/javase/7/docs/api/javax/swing/JTextArea.html). If you look to [JTextField limiting character amount input and accepting numeric only](http://stackoverflow.com/questions/12793030/jtextfield-limiting-character-amount-input-and-accepting-numeric-only), you will see what is probably a better approach. You are locking into a particular (probably incorrect) approach with JTextArea. – KevinO May 08 '16 at 17:40
  • It's for Handicapped persons so It's little importnant – Hen Shabat May 08 '16 at 17:41

2 Answers2

2

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();
    }
}
Lukas Rotter
  • 4,158
  • 1
  • 15
  • 35
0

Have a container, (HashSet, ArrayList, whatever you like), and have methods looking similar to this pseudocode for the key presses/releases

add:
    if(!list.contains("key to enter")) addKey();

remove:
    if(list.contains("key to remove")) removeKey();

then at the end of your methods, update the text area with the data from the list.