Solved

Updating a dialog while reading a file

Posted on 2004-03-30
25
162 Views
Last Modified: 2010-03-31
I have a dialog that starts up a new file reader object (that runs in a separate thread). That file reader object needs to update a textfield message box on the dialog as it progresses through the file. I tried  starting the dialog in new thread but the message box still doesn't update until the file reader object is done reading the file. Here's a code summary of what I have done:

Thread a;
Runnable run2 = new Runnable(){
    public void run(){
        //start up dialog that starts the file reader object (in another thread)
    }
};

a = new Thread(run2);
a.start();

Any ideas how I correctly update the textfield in the dialog from the different file reader object? Right now I pass a reference to the textfield to the file reader object.
0
Comment
Question by:cmp2
  • 8
  • 8
  • 6
25 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 10717456
Try something like:


// inner class
class FieldUpdater implements Runnable {
      public static void run() {
            yourDialog.setText("New text");
      }
}


// in your file thread (i.e. in run2) - update dialog:
invokeAndWait(new FieldUpdater())

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10717462
Sorry

SwingUtilities.invokeAndWait(new FieldUpdater());
0
 
LVL 92

Accepted Solution

by:
objects earned 125 total points
ID: 10717918
pass a reference to the text filed to your thread:

class MyThread implements Runnable
{
   JTextField TF = null;

   MyThread(JTextField tf)
   {
      TF = tf;
   }

   public void run()
   {
      ...
      // to update text field use:
      // TF.setText(s);
      ...
   }
}
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10721246
>> Runnable run2 = new Runnable(){

Thread run2 = new Thread () {

could also work.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10879176
I don't think that my comment would make much of a difference because complete solutions had already been provided. It was just another alternative which didn't make much of a difference.

Split between objects and CEHJ only.
0
 
LVL 92

Expert Comment

by:objects
ID: 10884387
theres no need to use invokeAndWait() in this case.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10884410
True but it doesn't hurt and encourages good habits for when it is required
0
 
LVL 92

Expert Comment

by:objects
ID: 10884428
unnecessaary object creation is *not* a good habit to encourage.
In fact it is a *bad* habit.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10884457
All that will happen, most probably, if the questioner is not aware of the working of interaction with the EDT, is that he'll go away and take the easy way in another context and wonder why he's got problems. It's the correct way to do it anyway as it happens. It just transpires that you can get away without it in this case.
0
 
LVL 92

Expert Comment

by:objects
ID: 10884526
Sorry but thats does not make any sense whatsever, what you have posted is not relevant to this question. And even if they were then you should explain the reasoning (but I guess there is none so whats to explain).
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10884566
I've done all the explaining that i'm going to do sorry. If you don't understand what i mean, maybe mayank will be kind enough to explain it to you
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 92

Expert Comment

by:objects
ID: 10884605
Explaination of what??? The point is your comment is irrelevant to the question.
So no explanation necessary.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10885911
Ah, cool it guys. Venabili, for some reason, I feel your initial recommendation was fine and I hope the question is closed as per that ;-)
0
 
LVL 92

Expert Comment

by:objects
ID: 10886310
> for some reason, I feel your initial recommendation was fine

Can you help me understand then how calling setText() from the event dispatch thread will make any difference then?
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10887490
>> I feel your initial recommendation was fine

Oh, I said that just hoping to close the arguments going on. I don't see a point arguing so much because the questioner is not listening.

>>  how calling setText() from the event dispatch thread will make any difference

So far, I had not been following the comments carefully on this page after the clean-up comment, but now that I do see them again, I think that the invokeAndWait () should not be used.
0
 
LVL 92

Expert Comment

by:objects
ID: 10887523
>  I don't see a point arguing so much because the questioner is not listening.

My concern in this case is more the PAQ, people reading the thread at a later date will be misled. (I don't see a point in arguing about it either)
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10887854
>> people reading the thread at a later date

Your concern about that is correct.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10887935
A point to you mayankeagle - and the last word on the matter from me - i too am concerned, believe it or not, about people being misled. That's why i am pointing out that it's NOT ok to change components from a non-EDT thread in Swing in most cases.
0
 
LVL 92

Expert Comment

by:objects
ID: 10887952
> That's why i am pointing out that it's NOT ok to change components from a non-EDT thread in Swing in most cases.

no one said it was but that has nothing to do with this question.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10888016
I know that you are concerned too, CEHJ ;-) I know that you always are.... both of you :-)

But as far as my knowledge goes, I don't see why invokeAndWait () would help much, and I also don't find what non-EDT thread in Swing has to do with this. Maybe that I know a little lesser or am unable to see something which is very apparent to both of you. Excuse my little knowledge compared to yours ;-)
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10888127
Don't be cross at me for something ;-) I hope you didn't miscomprehend me to be partial or something like that.

>> believe it or not,

You don't need to say that. I always believe in what you say.

I respect both of you equally, and your knowledge. Maybe that I don't know enough to speak more on this.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 11683332
I would still say, split objects and CEHJ, like I said before. This is the last comment from me on this page.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Protect jar file - windows app 2 39
Java / Linux and Regular Expressions 11 71
Java Timer (static) 9 36
expectj telnet failing 5 26
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now