0

I cannot write in TextArea and I don't know what to do in this case.

Here is my code from class "Server" that is used in order to put text in TA:

public class Server implements Runnable {
.
.
.


 @Override
public void run() {
    while (true) {
        try {
            if (queue.size() !=0) {
                Task task = queue.take();
                String text = "Server" + Integer.toString(getIdServer()) + " is processing the task: " + Integer.toString(task.getIdTask()) + " with proc time " + Integer.toString(task.getProcessingTime());
                InputShop.textArea.setText(text);
                Thread.sleep(task.getProcessingTime() * 1000);
                waitingTime.addAndGet(-task.getProcessingTime());
            }
        } catch (InterruptedException e) {
        }
    }
}

 public void addTask(Task newTask) {
        String text2 = "Task "+ Integer.toString(newTask.getIdTask()) + " arrives at s " + Integer.toString(newTask.getArrivalTime())+  " on server "+ Integer.toString(getIdServer());
        InputShop.textArea.setText(text2);
        queue.add(newTask);
        waitingTime.addAndGet(newTask.getProcessingTime());
}

And my UI Class looks at that part with TextArea like this:

        TextArea textArea = new TextArea();
        textArea.setPrefWidth(300);
        textArea.setPrefHeight(300);
        GridPane.setConstraints(textArea, 8, 8);
        gridPane.getChildren().add(textArea);
        textArea.setEditable(false);

If I write "System.out.println(...);" instead of the part with "InputShot.textArea.setText(...);" everything works fine, and it prints everything on the output log of the screen.

I have been searching a lot and struggling with this problem for hours and I do not know how to manage it, so I would appreciate every help!

If I missed an important part of code, or information, please let me know in order to edit the question!

  • 2
    You try to set the `TextArea` 's text in another thread than the Fx one. – Pagbo Apr 19 '18 at 21:50
  • 1
    See if you can bind the `TextArea's` `TextProperty` to the `Task's` `MessageProperty`. https://stackoverflow.com/questions/19968012/javafx-update-ui-label-asynchronously-with-messages-while-application-different – SedJ601 Apr 19 '18 at 21:53
  • 1
    Put your `while (true)` loop *inside* your `try` block. That way, your loop will exit when interrupted—which is what you want. An interrupt is an explicit request by other code for your thread to stop what it’s doing and exit gracefully. Code that ignores interrupts is a rogue thread. – VGR Apr 19 '18 at 22:23
  • Which thread are you running the server on? – James_D Apr 19 '18 at 22:37

0 Answers0