posted 12 years ago
Thanks again Ranga,
Hopefully, you will bear with me a little longer, as I am not quite getting how I should be working with threads. I think you may have hit upon my problem in that adding a status window was an afterthought and the rest of the program may not be well structured to support it. Though I guess I echo the thoughts from many other postings I have read on this subject in that it is quite frustrating that println() echos my messages exactly when I want them and it seems amazingly more difficult to have that same message appear in a text field at the desired time.
To simplify things a little, the program has an ObservableValue. The user can make changes in the interface and that results in an update of the observable. It then notifies its observers and, depending on what value changed, one or a number of calculations are done, some of which are a bit time consuming, with the net result that other portions of the interface are updated. I had a request to provide feedback to the user between when they made a change and when the GUI was updated with the final values. So I made this LogHandler that, in theory, would allow the behind the scenes processes to write to a JTextField when they started and finished and at appropriate times while they ran.
What happens in practice is that the user makes a change, the processes start up, I see their messages echoed in the Command Prompt window, but not the JTextField, and then when the processes finish, the JTextField updates. This is whether I just have the setText line, the setText as a Runnable I invokeLater, or fire it off as its own Thread. The only difference I see is that if I create a new Thread, the messages no longer echo to the command prompt window until after the other processes finish and Windows starts beeping at me while the processes run.
Thanks for pointing me at SwingWorker. Am I correct that to get the result I am looking for I need to convert all the methods that might need to be called (there are a hundred or so) to extend SwingWorker, so that instead of running in the event thread they are running in another thread which then publishes back to the GUI running in the event thread? The problem is that I doubt most of what I wrote is thread-safe.
I am running the whole GUI using invokeLater. Is part of the problem that I am trying to setText with invokeLater in the middle of a method that is running in the event queue, resulting in the updates not occurring until the end?
I'll try to put an example together that doesn't use an observable and see if I can better illustrate what I see happening. I'm on other non-Java/non-programming projects for the rest of the week. So it will be early next week.