Home arrow static arrow Java Programming [Archive] - print dynamically in a JTextArea
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - print dynamically in a JTextArea
1 Duke Stars available
This topic has 3 replies on 1 page.

Posts:1
Registered: 8/1/04
print dynamically in a JTextArea  
Aug 1, 2004 3:22 AM



 
Hi,
I've a problem;
I must print in a JTextArea a list of number the can be more or less long;
practically, in a for cicle I append numbers to the JTextArea;
but they are showed in JTextArea only at the end ofthe for cicle;
so, when I have more then 300000 numbers I receive the error "Out of memory".

How can I print in JTextArea the numbers one by one, before the end of the for cicle?
Is there a sort of Flush() method or simil?

Thank you in advance and excuse me for my poor english.
 

Posts:24,517
Registered: 98-02-27
Re: print dynamically in a JTextArea  
Aug 1, 2004 7:58 AM (reply 1 of 3)



 
There is a Swing forum for posting Swing related questions.

The "for loop" processing should be executed in a separate thread otherwise the GUI doesn't get any CPU to repaint the JTextArea. Check out this [url http://forum.java.sun.com/thread.jsp?forum=57&thread=437592]example[/url].
 

Posts:8,813
Registered: 10/4/00
Re: print dynamically in a JTextArea  
Aug 1, 2004 8:09 AM (reply 2 of 3)



 
You are asking a lot of questions here and you haven't posted any code to help anyone know more about what you are doing. First, you need to get your processing off the event thread with a new thread so the event thread can paint when it needs to. Second, you need to allow some time for the event thread to paint. If the processing is taking 100% of the CPU time, not much painting will happen. Third, you need to respect the 'Single Thread Rule' and only make updates to the Swing GUI on the event thread (invokeLater). Here's my code that shows this
import java.awt.*;import java.awt.event.*;import javax.swing.*;public class Test3 extends JFrame {  JTextArea jta = new JTextArea();  StringBuffer sb = new StringBuffer();  boolean counting = false;  JButton jb = new JButton("Start");  public Test3() {    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    Container content = getContentPane();    content.add(new JScrollPane(jta), BorderLayout.CENTER);    new Thread(new Runnable() {      public void run() {        int i=0;        while (true) {          if (counting) {            sb.append("The number is now ");            sb.append(i);            sb.append("\n");            if (sb.length() > 3000) {              sb.delete(0, sb.indexOf("\n", 1000));              SwingUtilities.invokeLater(new Runnable() {                public void run() {                  jta.setText(sb.toString());                }              });            }            i++;          }          try { Thread.sleep(10); } catch (Exception e) {}        }      }    }).start();    content.add(jb, BorderLayout.SOUTH);    jb.addActionListener(new ActionListener() {      public void actionPerformed(ActionEvent ae) {        counting = !counting;        jb.setText(counting?"Stop":"Start");      }    });    setSize(300,300);  }  public static void main(String[] args) { new Test3().setVisible(true); }}
 

Posts:8,813
Registered: 10/4/00
Re: print dynamically in a JTextArea  
Aug 1, 2004 8:45 AM (reply 3 of 3)



 
Oh yeah. I also snipped off the beginning of the string whenever it got over 3000 characters. And I used a StringBuffer so not so many Strings objects get created (Although there are still a lot). And I only redisplayed whenever I snipped the string (although that could be changed to every 10 numbers or whatever). Most people can't read over 1000 words per second.
 
This topic has 3 replies on 1 page.