Akhil Vijay Akhil Vijay - 6 months ago 21
Java Question

Why Java swing components failed to do typewriting effect

I am using netbeans8 for doing my java projects.

Here is a java function that I create it to perform some typing effect.Program shows no error,and working good.



Any one can copy this function in your program and run it.and view the result.there you can see the actual problem..Is it my error or any thing else.
Here is my bit of code

private void type_writer_effect() {
CERT[0]="10101101 1010101 101010101101 011001";
CERT[1]="10101101 1010101 101010101101 011001";
CERT[2]="10101101 1010101 101010101101 011001";
CERT[3]="10101101 1010101 101010101101 011001";
CERT[4]="10101101 1010101 101010101101 011001";
CERT[5]="10101101 1010101 101010101101 011001";
// Here simply i wrote an array named CERT[6]
// not an actual value.its just for testing

int i=0;
int j=0;
jTextPane1.setText(" ");
for(i=0;i<=5;i++) {

for(j=0;j<=CERT[i].length()-1;j++) {
// to display in console
System.out.println(jTextPane1.getText()+String.valueOf(CERT[i].charAt(j)));

// to display in jTextPane
jTextPane1.setText(jTextPane1.getText()+String.valueOf(CERT[i].charAt(j)));

if(CERT[i].charAt(j) == ' ') {
try {
Thread.sleep(20);
} catch (InterruptedException ex) {
System.err.println(ex);}
} else {
try {
Thread.sleep(10);
} catch (InterruptedException ex) {
// System.err.println(ex);}
}
}
jTextPane1.setText(jTextPane1.getText()+"\n");
}
}


private void jButton1MousePressed(java.awt.event.MouseEvent evt) {
type_writer_effect();
}


The result getting in the netbeans output panel looks great and exactly like what I need.

// to display in console
System.out.println(jTextPane1.getText()+String.valueOf(CERT[i].charAt(j)));


But result displaying in other java swing Components like
jTextpane
or
jLabel
or
jTextArea
does not get any typing effect.

// to display in jTextPane
jTextPane1.setText(jTextPane1.getText()+String.valueOf(CERT[i].charAt(j)));


Does anyone help me to display the same typing effect in
jTextarea
as shown in Output Panel ?

Answer

Try creating a Timer for doing the typing effect. Something like this:

  private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:

        Timer t = new Timer(100, new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                //System.out.println("J = " + j);  //for debugging
               // System.out.println("I = " + i);
                if(j >= CERT[i].length()){
                    j = 0;
                    i++;
                    jTextArea1.setText(jTextArea1.getText() + "\n");

                }

              if(i >= CERT.length) ((Timer)e.getSource()).stop();
              else{
                  jTextArea1.setText(jTextArea1.getText() + CERT[i].charAt(j));
                  j++;
              }
            }
        });
        t.setRepeats(true);
        t.start();
    }  

This way I'm thinking the UI thread wont get stuck when you call Thread.sleep.