Q:

Typing effect to text in JTextArea

I am making a program that has a JTextArea. I am using the append() method to add text to it. I want the text to be like someone is typing in the JTextArea, i.e. it should type one character, then wait for 400 milliseconds, the the next character, then again wait, and so on.
This is my code:
public void type(String s)
{
    char[] ch = s.toCharArray();
    for(int i = 0; i < ch.length; i++)
    {
        // ta is the JTextArea
        ta.append(ch[i]+"");
        try{new Robot().delay(400);}catch(Exception e){}
    }
}

But this is not working. It waits for some seconds, without displaying anything, and then displays the whole text at once. Please suggest.

A:

Use javax.swing.Timer instead. Keep reference to the JTextArea instance and char index. On each actionPerformed() call append current char to the JTextArea. When the char index equals to the char array length stop the Timer

