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.
the loop is triggered by a GUI button. please see the sample code below. this is just part of 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 < 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();
}
}
}
}
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.
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.
Can anyone please help me. Many thanks!
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…