JTextArea won't update
Could any of you please help me with this problem: I have a JTextArea that needs to update while my program is running. The method that should do the update, looks like this:
public void alg()
{
.....
for (int t=0;t<this.nr_gen1;t++)
{
String s=new String("t= "+t+"\n");
this.jta.append(s); // jta is the JTextArea
this.jta.updateUI();
try{
Thread.currentThread().sleep(600);
} catch(Exception e){
}
}
.........
}
Well, if I call this method from main, it updates the JTextArea while the program is running. But if I call it from a button's ActionPerformed method, the JTextArea won't update and the results are displayed in the JTextArea only when the program finishes.
I would be very grateful if you could help me with this. Thank you in advance.>
# 1
Hi juliana,
Try this:
First the string variable. If you use strings in a for loop you may better use an stringbuffer class. As you may know, the string class is not changable once it instantiated. So every loop the string class gets instaniated new. (It doesn't matter because the GC cleans up, but it's my opinion). The StringBuffer class is changable, so you can modify the string.
The method updateUI() is a notification from the UIFactory that the L&F has changed. If you write data in an JTextArea you don't change the UI of the TextArea. Try to use the repaint() method. It gets inherited from the JComponent class and it repaints the Component.
public void alg()
{
.....
for (int t=0;t<this.nr_gen1;t++)
{
// Replace the string class
String s=new String("t= "+t+"\n");
this.jta.append(s); // jta is the JTextArea
this.jta.updateUI();
// try the repaint "this.jta.repaint();"
try{
Thread.currentThread().sleep(600);
} catch(Exception e){
}
}
.........
}
Hope, this was helpfull. Let me know you final solution.
shape>
shapea at 2007-7-12 21:24:10 >

# 2
Quit multi posting you where given that answer in your last posting on this topic.Also, don't forget to use the "Code Formatting Tags",see http://forum.java.sun.com/help.jspa?sec=formatting,so the posted code retains its original formatting.