Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to display console output in java JTextarea one by one in a loop when button action is triggered
    primarykey
    data
    text
    <p>I got a problem with how to display console output in Jtextarea one be one. I have successfully redirected system console output into JTextarea. But the problem is that in real system console, the output show up one by one(i set a Thread.sleep() function, so the result will show up, say, every half seconds). But in JTextarea the output will show only once when the loop finish, it doesn't show one by one like the real system console.</p> <p>the loop is triggered by a GUI button. please see the sample code below. this is just part of code. </p> <pre><code>// Create a button. but.setVerticalTextPosition(AbstractButton.CENTER); but.setHorizontalTextPosition(AbstractButton.LEADING); but.setActionCommand("publish"); but.addActionListener(this); // Button action public void actionPerformed(ActionEvent e) { final JButton source = (JButton)e.getSource(); if(source.equals(but)){ for( int i = 0 ; i &lt; 5 ; i++ ) { System.out.println( i ); // regular textarea output //JTextarea.append(Integer.toString(i)); try { Thread.sleep( 500 ); } catch (InterruptedException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } } } </code></pre> <p>as you can see, i use System.out.println( i ) in the loop, because i have redirected system console output into JTextarea, so the output is in JTextarea. </p> <p>the problem is, like I mentioned above, in real console, the output show one by one every 500 milliseconds. But in redirected Jtextarea, the result shows once when all the loop is done. I don't know why it is like this. I want the output to show one by one as well in redirected JTextarea.</p> <p>Can anyone please help me. Many thanks!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload