Can I create a thread without a function in Java?

I need a parallel function that works all the time in my program. I do not know if I think correctly, but what I want to do is to constantly update the TextArea code, receiving information from the server.

I use RMI, just you know.

Can I create a stream function in MainClass and let it work all the time?

Or how can I create a thread to update my TextArea in another class? How to share your TextArea?

+3
source share
1 answer

Not sure what I understand, I assume that TextArea means that JTextArea and MainClass are the entry point of the application.

What is stopping you from doing this like that?

public class Updater implements Runnable {
    private JTextArea textArea;

    public Updater(JTextArea textArea){
        this.textArea = textArea;
    }

    @Override
    public void run(){
        SwingUtilities.invokeLater(new Runnable(){
            public void run(){
                            //do what you've got to do....
                textArea.setText("New Text");
            }
        });
    }

}

And in your "MainClass" something like this:

public static void main(String[] args) {

    Thread myThread = new Thread(new Updater(myTextArea));
    myThread.start();

}
+3
source

All Articles