• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem with JTextArea

 
Kuppusamy Venkatasubramanian
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am working on a GUI development for back-end processing. In that i have a JPanel and few UI components. The user selects the file to be processed and click Submit. I have a JTextArea (below the JPanel and part of the same JFrame which acts as a console) and on click of the Submit button, the JTextArea should update the log of the processing in real-time. But it is not. It gets updated at the end of the back-end processing. I googled and tried various solutions using SwingWorker. Nothing worked. May be i am missing something.

The code is as below.

I have a main class where i create JPanel add components of JTextFields, Buttons etc. I add the JPanel to the JFrame. I add the actionlistener for the button. Once the submit button is clicked, i start the backend processing and get result in each step. I create a SwingWorker class and push the result to the JTextArea. But it is not getting updated. Please help. Below is the code snippet.

appendLog is called at each step of back-end processing. If there is any alternative way to display the processing log, please, advise. I tried JTextPane also, same issue.



This LogToConsole is an inner class within the main class. log is referring to the JTextArea.



Thanks in advance.
Samy..
 
Rob Spoor
Sheriff
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you show us the code that is calling appendLog? Something tells me you're still doing all of the work on the Event Dispatcher Thread, and only doing the appending using the SwingWorker. That doesn't make any sense. It should be the other way around; the hard work should go in the doInBackground method.

Also,
Kuppusamy Venkatasubramanian wrote:
You are appending the full text each time you log something. That means that the text area's text will be duplicated upon each log.
 
Kuppusamy Venkatasubramanian
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Thanks for your response. I will try reverting that the main processing happens in doInBackground and the logging happens in the actionPerformed method and will post the result. Regarding the calling of appendLog, it happens as below inside actionPerformed method which is invoked when submit button is clicked.

PrepareSendBatch _batch = new PrepareSendBatch();
String verifyBatchResult = _batch.verifyBatch();
appendLog(verifyBatchResult);
String zipResult = _batch.zipFile();
appendLog(zipResult);
.
.
.
.

For log.append(log.getText()+"\n"+chunk); , i added for testing and it should be log.append(chunk+"\n");

Thanks
Samy..
 
Rob Spoor
Sheriff
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm guessing that both _batch.verifyBatch() and _batch.zipFile() are long operations, and these should not be executed directly in the actionPerformed method. These two calls are what needs to go into doInBackground:
Inside the actionPerformed method you replace the current content and replace it with the one single SwingWorker you execute.
 
Kuppusamy Venkatasubramanian
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

It does work with this implementation and the JTextArea is updated in real-time. Thanks for you help.

Thanks,
Kuppusamy.V.,
 
Rob Spoor
Sheriff
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic