File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Update GUI components from another thread.

 
will phillips
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It has been a while since I last had the chance to do anything with Swing or AWT, so I am a little bit rusty to say the least, so be kind and bare with me.

I have a program that I wrote a while back that I would like to replace the console output with a simple GUI. The original program fills a number of arrays with information from several files, prints an initial state, then starts several threads that as they change the contents of the arrays, they print the changes to the console. I would like to replace the console output with something that tells the GUI to update a specific components or all components. The first couple of things were easy, I have a new program that loads the arrays with the information from the files and a GUI that fills several JLists based on those arrays.

Right now I am wondering if there is anything I can do within the original threads that will allow me to update the GUI or if it would be better to start a SwingWorker that monitors the arrays and updates the JLists while the other threads are running.

 
Campbell Ritchie
Sheriff
Pie
Posts: 47229
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
will phillips wrote: . . . anything I can do within the original threads that will allow me to update the GUI or if it would be better to start a SwingWorker . . .
Swing API wrote:Swing's Threading Policy
In general Swing is not thread safe. All Swing components and related classes, unless otherwise documented, must be accessed on the event dispatching thread.
here
So why have you got "threads" accessing your Swing components in the first place?
 
will phillips
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:So why have you got "threads" accessing your Swing components in the first place?


At the moment they don't and since Swing is not thread safe, I obviously shouldn't either. What I was asking is, is there anything that can be done from my threads that would be safe to do, such as adding something to the event dispatching thread that could update the JLists. If not, what is the proper way to ensure that a JList or any component really, correctly reflects a changing dataset?
 
Paul Clapham
Sheriff
Pie
Posts: 20166
24
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
will phillips wrote:At the moment they don't and since Swing is not thread safe, I obviously shouldn't either. What I was asking is, is there anything that can be done from my threads that would be safe to do, such as adding something to the event dispatching thread that could update the JLists. If not, what is the proper way to ensure that a JList or any component really, correctly reflects a changing dataset?


You are absolutely correct about what you should be looking for. And Concurrency in Swing is where you should be looking for it.

(Hint: that link is the first one which Google returns for keywords "swing concurrency tutorial".)
 
Rok Štelcer
Ranch Hand
Posts: 101
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

If I understood your question/problem correctly, you're looking for:
  • SwingUtilities.invokeAndWait
  • SwingUtilities.invokeLater

  • So, in case you have any part of the functionality in a background/worker thread, which has anything to do with the GUI visualization per se, it should be put on the EDT via above wrappers.

    Another way, as you already purposed, is to use SwingWorker.


    Regards,
    Rok
     
    Mikko Kohtamäki
    Ranch Hand
    Posts: 112
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    In SwingWorker
  • doInBackground - work that has to be done in background (in a separate worker Thread, not in EDT)
  • publish - called from doInBackground to send chunks of information to be processed in EDT via process method
  • process - as I mentioned all code in process method is executed in EDT, and here you can update your Swing component


  • And yes, all this can be found at SwingWorker documentation
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic