Forums Register Login

SwingWorker inside SwingWorker

+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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!
"To do good, you actually have to do something." -- Yvon Chouinard
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 2246 times.
Similar Threads
focusListener wont work on JComboBox
Design Questions
JTable Cell Editor
Is this good approach to design DB i/f?
Does stateful or stateless session bean make sense ?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 02:14:53.