• Post Reply Bookmark Topic Watch Topic
  • New Topic

SwingWorker (thread) + Http Post (call) >> returned nothing?  RSS feed

 
J. Insi
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey everyone!

I tried to implement Http Post Call inside a thread called SwingWorker.
Because if I put it under common Thread the GUI seems freeze!

Basically I have a button that if we clicked it, it will execute this code:




You might confused where is the Submitter class comes from?
Here it comes.



But the funny thing is,
that the getLastResult() when the button is clicked never returned me anything. Is this because of the while loop that never ends?
Can anyone help me a bit, how should it be? Because the main point why i put SwingWorker (thread) + Http Post (call) is because
I need the callback (returned value) once the button is clicked and request something to the web.

CMIIW
 
Chris Hurst
Ranch Hand
Posts: 443
3
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok ...



will almost certainly be called before the task finishes, i.e. what you would want to do to make your code work is wait for the worker to finish then do the println but this would lock your GUI up also bad so take out that println you don't want it.

The println should sit in your done () method, in reality you will want to update your GUI here eg a text label to say hey the result was ... , the one trick you'll find is println will work in done() updating the GUI may work badly as you should only update that from the EDT thread Google Swingutilities.invokeLater for some examples.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Chris, except for this part...
Chris Hurst wrote:the one trick you'll find is println will work in done() updating the GUI may work badly as you should only update that from the EDT thread Google Swingutilities.invokeLater for some examples.


The done() method is actually executed in the event dispatch thread (the GUI thread) so it is the place you should use to update the UI.
 
Chris Hurst
Ranch Hand
Posts: 443
3
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The done() method is actually executed in the event dispatch thread (the GUI thread) so it is the place you should use to update the UI.


Ah , I haven't done any Swinging for a while ;-) , nice catch.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!