This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

SwingUtilities.invokeLater problem  RSS feed

 
sandeep kumar jangra
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi frenz.. i m making an application in which I m calling servlets from Swing applications. these servlets are talking to database and performing some operations. so far fine.
these opeartions are time consuming.. so I thought of using SwingUtilities. invokelater(). but I want to get return values from these operations which will be taking place in runnable objects run method. compiler is saying these values should be final. but i want to use these values in other parts of the programs.
i.e. after servlet is done with its work.. it returns a value... I want to work with that value...plz help if anybody have any idea. plz make sure reply is easy to understand coz i m not perfect.
thanx
 
Mike Rane
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try making the local variables you want to access in the inner class instance variables of the outer class.
 
Seb Mathe
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take care : using SwingUtilities.invokeLater will cause your code to be executed on the event dispatching thread, I think it will be better if you create your on Thread...

I'm not sure to understand your needs concerning the objects that you want to deal with after your thread return, but maybe you should have a look on the consumer / producer sample in the Java tutoriel (Threads section)
 
Charles Woodson
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As someone already said, look at the Threads page

There is a link on the page to the useful SwingWorker class
http://java.sun.com/docs/books/tutorial/uiswing/misc/example-1dot4/SwingWorker.java
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can also create a version of your variables for the new Thread to play with. For example:



If you want to actually modify a variable in your new Thread, then use it later in the mehod you started that Thread from it's another story, especially if you need the new Thread to complete it's task before you do. In your case it may be more appropriate to have a callback. For example:

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!