Rewrite java textarea content

i add text to the text area for every second, i would like to overwrite or clear the old text and i want to write new data every second, how to do this in java?

Thanks raksha

+2


source to share


2 answers


I think you are talking about Swing JTextArea

.

You can just call setText(...)

on it to replace the text:



JTextArea textArea = ...;

textArea.setText("Hello World");

      

+2


source


You need a thread to do things periodically, but be aware of using SwingWorker . Unless your GUI gets frozen.



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

      

0


source







All Articles