java点击按钮在控制台输出,当触发按钮操作时,如何在循环中逐个显示java JTextarea中的控制台输出...

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!

解决方案

To make Swing GUI to update dynamically on fire of some event or with some continuous background changes , you can use SwingWorker API provided by Swing. Try this Code :

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class DynamicWrite implements ActionListener

{

JFrame frame = new JFrame("TextArea");

JTextArea tArea = new JTextArea(10,20);

JButton button = new JButton("Click");

JScrollPane pane = new JScrollPane(tArea);

SwingWorker worker;

String s= "Java is an Object Oriented Programming langauge...Java is static typed language...asbfldfjsdj";//some random String

public void prepareAndShowGUI()

{

Container container = frame.getContentPane();

container.add(pane);container.add(button,BorderLayout.NORTH);

tArea.setLineWrap(true);

tArea.setWrapStyleWord(true) ;

button.addActionListener(this);

frame.pack();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setVisible(true);

}

public void actionPerformed(ActionEvent evt)

{

if(evt.getSource()==button)

{

tArea.setText("");

if (worker!=null)

{

worker.cancel(true);

}

worker = new SwingWorker()

{

@Override

protected Integer doInBackground()//Perform the required GUI update here.

{

try

{

for(int i = 0;i

{

tArea.append(String.valueOf(s.charAt(i)));

Thread.sleep(5);

}

}catch(Exception ex){}

return 0;

}

};

worker.execute();//Schedules this SwingWorker for execution on a worker thread.

}

}

public static void main(String st[])

{

DynamicWrite dyna = new DynamicWrite();

dyna.prepareAndShowGUI();

}

}

I hope this solves your problem.

To know more about SwingWorker API watch here

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值