overwrite content of textarea java

2019-02-25 21:39发布

问题:

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

回答1:

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");


回答2:

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();


标签: java core