File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How can I update a JTextField in threaded code?

 
Mike London
Ranch Hand
Posts: 1158
1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've got an actionPerformed method that scans though directories. I need to update a JTextField with each file to show what file is being searched.
Without a spearate thread, as I've found, the JTextField won't get updated until the loop returns (the loop is started in the actionPerformed() method of the "Search!" button).
What I don't understand is how can I create a separate thread to update the JTextField when in that "run()" method, I need to have access to the JTextField control? Since the run() method is in another class, the JTextField in the outer class is hidden.
What I'm saying is that the run() method will hide the JTextField from the outer class.
For example, if I create an inner class like this:
class testme implements Runnable
{
public void run()
{
// CAN'T SEE FORM CONTROLS FROM OUTER CLASS
}
}
Do I need to pass in an object reference from the actionPerfomed() method from the main class to the constructor of the threaded object (with the run() code) so it can access the JTextField to set its display with setText()?
I can't find any examples that try to update a display in a separate thread like this.
Thanks for anyone's input.
Mike.
 
Chantal Ackermann
Ranch Hand
Posts: 508
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how's this?

the above method gets called in your runnable.
is that the answer to your question or have I missed some point?
c
 
Mike London
Ranch Hand
Posts: 1158
1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply.
I finally got it working by just removing the EventQueue.InvokeLater() altogether.
With just calling a separate thread and having that thread update the text field (from the run method), it works as expected.
Thanks again.
Mike
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic