overwrite content of textarea java

2019-02-25 21:10发布

i appending text to text area for every sec i wanted to overwrite or clear the old text and i want write new data for every one sec how to do this in java?

Thanks raksha

标签: java core
2条回答
一纸荒年 Trace。
2楼-- · 2019-02-25 21:28

To do something periodically you need some thread, but be aware to use SwingWorker. If not your GUI may freeze.

        final JTextArea ta = frame.getjTextArea1();

        SwingWorker worker = new SwingWorker() {

            @Override
            protected Object doInBackground() throws Exception {
                while (true) {
                   ta.setText("");
                   ta.setText(new Date().toString());
                   Thread.sleep(1000);
                }
            }
        };
        worker.execute();
查看更多
ら.Afraid
3楼-- · 2019-02-25 21:32

I guess you are talking about a Swing JTextArea.

You can just call setText(...) on it to replace the text:

JTextArea textArea = ...;

textArea.setText("Hello World");
查看更多
登录 后发表回答