Speaky
Speaky

Reputation: 185

using java swing Timer to process and append text into textarea

I have a JTextArea txtProcess and 2 methods to process:

First method is to process multiplying numbers less than 10 using for loop. After multiplying them, all results will be appended to txtProcess using Timer.

I use timer to delay the appending. For example, first result appended to txtProcess. Then 500 miliseconds later, second result appended to txtProcess. And so forth until all results appended to txtProcess.

Like this:

int a = 10;
int result = 0;
for(int i=1; i <= a; i++){
    result = i * a;
    txtProcess.append("Result "+ i +" = "+ result);
}

Below is piece of code I've tried for first method.

    void first(){
        ActionListener listen = new ActionListener(){
            public void actionPerformed(ActionEvent e){
                for(int i=1; i <= a; i++){
                    result = i * a;
                    txtProcess.append("Result "+ i +" = "+ result +"\n");
                    if(i == 9){
                        ((Timer)e.getSource()).stop();
                    }
                }
            }
        };    
        Timer mine = new Timer(500, listen);
        mine.start();
    }

But, it doesn't work as I expected. I expected the results append to txtProcess one by one, not simultaneously. This is the first problem. How can I fix this?

When all process in first method already executed, the process continue to second method.

There is time interval between process of first method to second method.

I mean like this: after first method execution is finished, the second method execution will be started 2 seconds later. As you see, the time interval is 2 seconds (or probably longer).

So, I tried like this:

    void second(){
        ActionListener listen = new ActionListener(){
            public void actionPerformed(ActionEvent e){
                for(int i=1; i <= a; i++){
                    result = i * a;
                    txtProcess.append("Result "+ i +" = "+ result +"\n");
                    if(i == 9){
                        ((Timer)e.getSource()).stop();
                    }
                }
            }
        };    
        Timer mine = new Timer(500, listen);
        mine.start();
    }

Then I created another method to combine both of them:

       void combine(){
            ActionListener listen = new ActionListener(){
                public void actionPerformed(ActionEvent e){
                    first();
                    second();
                }
            };    
            Timer mine = new Timer(500, listen);
            mine.start();
        }

But, both first and second method executed simultaneously. This is the second problem: to create interval time between first method and second method. How can I fix this?

Note: You might think this question is duplicated with java for-loop in GUI TextArea. I already read and tried the code there, but it still can't fix the problems.

Upvotes: 0

Views: 721

Answers (1)

Robin
Robin

Reputation: 36621

What the Timer does is executing the code in the ActionListener each time the interval is reached. So if you want text to append 10 times, you must not have a for loop inside your listener. The timer will take care of the looping for you.

ActionListener listener = new ActionListener(){
  private int counter = 0;
  @Override
  public void actionPerformed( ActionEvent e ){
     txtProcess.append("Result "+ counter +" = "+ result);
     counter++;
     if ( counter == 10 ){
       ((Timer)e.getSource()).stop();
     }
  }
}
Timer timer = new Timer( 500, listener );
timer.start();

I haven't carefully checked the code above, so it might contain a syntax error or loop just one time too much / one time less then needed. It serves more to illustrate the usage of the Timer.

Upvotes: 1

Related Questions