• Post Reply Bookmark Topic Watch Topic
  • New Topic

Text Area not getting updated with the button action performed event

 
Prem Swaroop
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have created a Text Area where I want to display some lines frequently as soon as the button is clicked. But the problem is that the Text Area is updated only once after the button action performed event is completed. Below is the way I used to display some text :-

private void buttonconnectActionPerformed(java.awt.event.ActionEvent evt){
Some code...
jTextArea1.append("Creating download and upload script in local drive...\n");
jTextArea1.setCaretPosition(jTextArea1.getDocument().getLength());
Some code...
jTextArea1.append("Files created\n");
jTextArea1.setCaretPosition(jTextArea1.getDocument().getLength());
Some code...
}

The whole program is creating, uploading, downloading some files to another server. So, that takes some time to complete. I want this text area to display the lines as the code progresses inside the button event.
 
Darryl Burke
Bartender
Posts: 5153
11
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Prem, and welcome to the Ranch!

You need to go through the tutorial on Concurrency in Swing.
 
Prem Swaroop
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Darryl for your reply. With the help of SwingWorker, my problem is solved. But still I have one doubt. First I tried to append TextArea in the process() method by the frequent calls to publish() method in the doInBackground() method. Below is the code how I tried first :-

But this did not work. The TextArea did not display anything. As far as I understood from the concept of SwingWorker is that the 2nd arguement is used for returning intermediate results. Can you please explain why it did not work.

Then I did this without publish() and process() method i.e appended TextArea directly inside doInBackground method().
 
Rob Spoor
Sheriff
Posts: 20819
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You didn't override process, you overloaded it. The correct method signature for your class is protected void process(List<String> chunks). This method takes a List because it's possible that you call publish several times before process is called. So fix the parameter type, then loop through the List to add all chunks.

Also, can you please UseCodeTags next time? Thanks.
 
Ranganathan Kaliyur Mannar
Bartender
Posts: 1103
10
Java Netbeans IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:You didn't override process, you overloaded it.


this is one place where the usage of @Override annotation would have helped you find the problem rightaway.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!