File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Problem with JTextArea Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Problem with JTextArea" Watch "Problem with JTextArea" New topic
Author

Problem with JTextArea

Kuppusamy Venkatasubramanian
Ranch Hand

Joined: Dec 30, 2004
Posts: 91
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..


SCJP1.4, SCBCD 1.3, SCWCD 1.4, SCEA 5, JLPT-N3
My Blog
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Kuppusamy Venkatasubramanian
Ranch Hand

Joined: Dec 30, 2004
Posts: 91
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

Joined: Oct 27, 2005
Posts: 19670
    
  18

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

Joined: Dec 30, 2004
Posts: 91
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

Joined: Oct 27, 2005
Posts: 19670
    
  18

You're welcome.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Problem with JTextArea