M
M
Mikhail2017-06-04 18:04:18
Java
Mikhail, 2017-06-04 18:04:18

How to update TextArea in Swing Java?

Actually, in a separate thread, I constantly check for a change in the sendToView and goods lines, but the application also freezes / crashes in the same way, etc. When I tested and immediately entered random text into the TextArea at startup, it was not displayed, but programmatically showed that everything was there (logTextArea.getText () is not empty, with the desired text). What needs to be changed?

class SomeThing extends App implements Runnable       
    {
        public void run()	
        {
        while(1==1){
        if (sendToView != "" || goods != "") if (sendToView != "") {
            logTextArea.append(sendToView);
            sendToView = "";
            System.out.println(logTextArea.getText());
        } else if (goods != "") {
            goodTextArea.append(goods);
            goods = "";
        }
    }
}
}

public class App {
...
    String sendToView = "";
    String goods = "";
    static SomeThing mThing;
...
public static void main(String[] args) {
        JFrame frame = new JFrame("App");
        frame.setContentPane(new App().panelMain);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
        mThing = new SomeThing();
        Thread myThready = new Thread(mThing);
        myThready.start();


    }

}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nikita, 2017-06-07
@jkotkot

You should have figured out comparing strings and objects in general before getting into the UI and multithreading. Your code is just terrible because you made every possible mistake in those few lines.
Well, in fact - you can work with graphics only from EventThread because it is not thread-safe. read how to run code on the graphics thread. see SwingUtilities.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question