tags:

views:

247

answers:

2

Hi, 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  A: 

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");
Jesper
i tried but its appending
Are you sure it is appending? Do some debugging: Check what you are passing to setText(). Is it the new text only, or are you setting it to old text + new text?
Jesper
its working thanks
Note that you can mark an answer as the accepted solution on StackOverflow, if you think it's the right answer to your question.
Jesper
A: 

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