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 Update GUI components from another thread. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Update GUI components from another thread." Watch "Update GUI components from another thread." New topic
Author

Update GUI components from another thread.

will phillips
Greenhorn

Joined: Oct 10, 2005
Posts: 11
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

Joined: Oct 13, 2005
Posts: 38818
    
  23
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

Joined: Oct 10, 2005
Posts: 11
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
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

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

Joined: Nov 03, 2009
Posts: 101
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


    SCJP, SCWCD
    Mikko Kohtamäki
    Ranch Hand

    Joined: Dec 13, 2008
    Posts: 112
    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
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Update GUI components from another thread.