title: Change the image in ImageView in javaFX
body: <p>I'm looking for an example where is needed to <strong>change</strong> <strong>dinamically</strong> the image <code>x.png</code> when an certain variable <code>x</code> getting value 1 into <code>y.png</code>, when this variable going back to 0 restore the old image. If this is possible , how? thanks for help </p>
  
  <p>im trying whith this code but have no succes... </p>
  
  <p>this is my controller class</p>
  
  <pre><code>public class FXMLDocumentController implements Initializable {
  
  @FXML
  private ImageView myImage;
  
  @Override
  public void initialize(URL url, ResourceBundle rb) {
      Random random = new Random();
  
  
      while (true) { // &lt;-- this is the problem how i can do that without while(true)??
          int x = random.nextInt(1);
          if (x == 0) {
              Image image = new Image(getClass().getResource("x.png").toExternalForm());
              myImage.setImage(image);
          }
          if (x == 1) {
              Image image = new Image(getClass().getResource("y.png").toExternalForm());
              myImage.setImage(image);
          }
  
      }
  
  }
  </code></pre>
  
  <p>}</p>