• Post Reply Bookmark Topic Watch Topic
  • New Topic

SwingWorker inside SwingWorker  RSS feed

 
J. Nuno
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

What is the correct way to do the following? I've got a panel which is a typical search criteria and results form. Before doing setPanel(myPanel), I need to populate a combobox with values from the db and then populate a table with results also coming from the db. The method to populate the table first gets the select combobox item to use as a search criteria. First I tried the following:



But this is wrong because it might happen that the 2nd swingworker.work() runs first than the 1st swingworker.updateUI() which will cause wrong behaviour. I know several ways to solve this but:
1) I don't want to use SwingUtilities.invokeAndWait() to populate the combobox
2) I don't think doing a .get() to the 1st SwingWorker is correct
3) I don't know if putting the 2nd swingworker inside the updateUI() method of the first one is ok?

This must be a typical use case so you guys must know how to do it properly. Thanks in advance!
[ September 23, 2008: Message edited by: J. Nuno ]
 
Rob Spoor
Sheriff
Posts: 20903
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, where did you get this SwingWorker class from? It's not javax.swing.SwingWorker, that's for sure. That uses doInBackground, done and execute instead of work, updateUI and start.

But to get back to your questions:
Originally posted by J. Nuno:
1) I don't want to use SwingUtilities.invokeAndWait() to populate the combobox
2) I don't think doing a .get() to the 1st SwingWorker is correct
3) I don't know if putting the 2nd swingworker inside the updateUI() method of the first one is ok?

1) Why not? That's most likely what SwingWorker does with its updateUI method.

2) Since I don't know this SwingWorker class I can't help you with that.

3) Since updateUI is (most likely) executed on the Event Dispatcher Thread starting the second SwingWorker sounds like a safe plan to me. You execute it in the same thread as now, only after the combo box has been updated. So it would do exactly what you need: start the second after the first one has finished.
 
J. Nuno
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your answer!

My comments:


1) Why not? That's most likely what SwingWorker does with its updateUI method.

2) Since I don't know this SwingWorker class I can't help you with that.

3) Since updateUI is (most likely) executed on the Event Dispatcher Thread starting the second SwingWorker sounds like a safe plan to me. You execute it in the same thread as now, only after the combo box has been updated. So it would do exactly what you need: start the second after the first one has finished.


1) Because using invokeAndWait forces me to try catch and to check if isEDT. Besides I think SwingWorker uses invokeLater, that's why I started this thread

2) Sorry about this confusion! I decided to post it like that because there are different SwingWorkers and I'm not using the JavaSE6 one, I though if I gave meaningfull names to the methods like work() and updateUI() it would be easier to understand

3) I also think so but I wanted an expert opinion. Glad to know you also think this works.

Thanks once again and best regards!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!