Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

JFrame update 1

Status
Not open for further replies.

brinker

Programmer
May 31, 2001
48
CA
Hi everyone,

I was wondering if anybody could help me with this problem. I have a main class which extends JFrame and it contains a JTextArea. The main class makes a call to another class which performs some processing in a looping manner:

while (s.hasMore())
{
urlsCompleted.setText(s.getCompleted());
repaint();
urlsToDo.setText(s.todo.toString());
urlErrors.setText(s.getErrors());
s.doNextSite();
}

However, the JTextArea does not dynamically update as the process is running. The only update on the produced GUI occurs once everything has finished running.

Does anybody know how to correct the problem so that the JTextAreas in the JFrame update dynamically. repaint() does not do the job.

thanks

Brinker
 
Try using
Code:
JTextArea.append(String s)

This appends the String s to the end of the document in the TextArea. Using \n should give you a new line if that is what you are after. ----------------------------------------
There are no onions, only magic
----------------------------------------
 
Thanks for the response JFryer.

Actually, the problem is not with just getting a new line, but refreshing teh JTextArea in general. The JTextArea remains frozen until the entire program is completed, while the string for the JTextArea is being dynamically refreshed (in a looping fashion). I wonder if there is a way to capture the information in the string, send it to the JTtextArea and be able to view it dynamically?

thanks

Brinker
 
Hi brinker,
If you are not setting the text for the
Code:
JTextArea
using
Code:
setText (String)
in the while loop, then the
Code:
JTextArea
will never know it needs to update. I do not know what
Code:
urlsCompleted
,
Code:
urlsToDo
or
Code:
urlErrors
reference so I can't say further, but I believe this is your problem.

Hope this helps,
MarsChelios
 
I believe this is to due to Threads. Your thread has a higher priority than the AWT thread that is responsible for drawing your GUI hence it executes then the AWT thread does. I think you may just want to call paint after acquiring the Graphics being used instead of scheduling a call to paint using repaint(). Also another solution I think would be to add the line:
Thread.sleep(milliseconds);
where milliseconds is the minimum number of milliseconds you wish to have your thread pause. This should allow for the AWT thread to update the UI. This is other solution only applies if your class doing the processing is implemented as a second thread. If not, I'd say just:
Graphics g = this.getGraphics(); // I think this is right
paint(g);

Hope that helps some
JavaDude32
 
JavaDude,

you're a genius. Everything works now with the declaring Graphics g=this.getGraphics(); then paint(g);

Thanks also to everyone who tried to help.

Brinker
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top