aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes How can I update a JTextField in threaded code? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How can I update a JTextField in threaded code?" Watch "How can I update a JTextField in threaded code?" New topic
Author

How can I update a JTextField in threaded code?

Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1083
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

Joined: Sep 28, 2000
Posts: 508
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

Joined: Jul 12, 2002
Posts: 1083
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
 
subject: How can I update a JTextField in threaded code?