title: JavaFX & GUI-Threads: Change button text from Thread
body: <p>I have a GUI with a TextArea and a Save Button. When I press the latter the text is saved. This takes around 2 seconds. During the process of saving the buttons should get another caption than before and after saving.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>    saveButton.setText("Saving...");
      Util.print("Saving...");
  
      Thread saveIt = new Thread(new Runnable() {
          @Override public void run() {
              Platform.runLater(() -&gt; {
                  try {
                      Thread.sleep(2000);
                  } catch (Exception ex) {
                      Util.print(ex);
                  }
                  saveButton.setText("Saved!");
                  Util.print("Saved!");
              });
          }
      });
      saveIt.setDaemon(true);
      saveIt.start();
  </code></pre>
  
  <p><strong>What happens:</strong></p>
  
  <p>The following output is produced on the command line after pressing the button:</p>
  
  <pre><code>Saving...
  Saved!
  </code></pre>
  
  <p>The command line prints "Saving..." directly after I click on saveButton. 2 seconds after pressing saveButton the command line prints "Saved!" and the button caption changes to "Saved!".</p>
  
  <p><strong>What I would expect:</strong></p>
  
  <p>The command line output <strong><em>and the button caption</em></strong> show "Saving..." directly after I click on the save button. After 2 seconds the caption changes to "Saved!".</p>
  
  <p><strong>How can I achieve the expected behaviour?</strong></p>
  
  <p>Thank you very much in advance for your help.</p>
  
  <p>P.S.: I know so many people have had problems with changing GUI elements from Threads. I already read some articles on StackOverflow &amp; the web about it, but this one's a too hard nut for me. Just for reference, here is some of the things I tried so far, among others Tasks:</p>
  
  <p><a href="https://stackoverflow.com/questions/20497845/constantly-update-ui-in-java-fx-worker-thread">Constantly Update UI in Java FX worker thread</a></p>
  
  <p><a href="https://stackoverflow.com/questions/17850191/why-am-i-getting-java-lang-illegalstateexception-on-javafx">Why am I getting java.lang.IllegalStateException &quot;Not on FX application thread&quot; on JavaFX?</a></p>
  
  <p><a href="https://stackoverflow.com/questions/11690683/javafx-update-ui-from-another-thread">javafx, update ui from another thread</a></p>
  
  <p><a href="http://blog.axxg.de/javafx-ui-thread-update/" rel="nofollow noreferrer">http://blog.axxg.de/javafx-ui-thread-update/</a></p>