raksha
raksha

Reputation:

overwrite content of textarea java

I am 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?

Upvotes: 1

Views: 6022

Answers (2)

PeterMmm
PeterMmm

Reputation: 24630

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

Upvotes: 0

Jesper
Jesper

Reputation: 206926

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

Upvotes: 2

Related Questions