vendredi 21 octobre 2016

Add random words at runtime inside jtextArea without using keyboard

As title said, i want to add at runtime words inside jtextArea, i simply wrote this:

import java.awt.*;
import javax.swing.*;

public class Test extends JFrame {

    private static final long serialVersionUID = 1L;
    private JTextArea tarea;

    public Test() {
        tarea = new JTextArea(10, 10);
    }

    private void init() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        insertRandomLetterInsideJtextArea();
        JScrollPane scroll = new JScrollPane(tarea);
        getContentPane().add(scroll, BorderLayout.CENTER);
        pack();
        setLocationByPlatform(true);
        setVisible(true);
    }

    private void insertRandomLetterInsideJtextArea() {
        while (true) {
            tarea.setText("foo\n");
        }
    }

    public static void main(String... args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new Test().init();
            }
        });
    }
}

but it don't work. The while(true) doesn't permit to show anything. Someone can explain me why?




Aucun commentaire:

Enregistrer un commentaire