This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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.
SCJP1.4, SCBCD 1.3, SCWCD 1.4, SCEA 5, JLPT-N3
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.
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.
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.
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.
Joined: Dec 30, 2004
It does work with this implementation and the JTextArea is updated in real-time. Thanks for you help.