title: handle mouse move and click together in javafx
body: <p>I'm trying to build a javafx app in which i need to respond to mouse movements and clicks together just like what happens in counter strike when you shoot. But the problem is when i press the mouse button it will not respond to mouse movements anymore until i release the mouse button. I want them both to work together in parallel. I tried to set my listeners in separate threads but it doesn't work.This is an image of a gun pointer.</p>
  
  <pre><code>Image image = new Image("/pointer.png");  // a 25*25 PNG icon
  ImageView imageView = new ImageView(image);
  scene.setCursor(Cursor.NONE);
  </code></pre>
  
  <p>and then :</p>
  
  <pre><code>scene.setOnMouseMoved(e -&gt; {
          imageView.setX(e.getX());
          imageView.setY(e.getY());
      });
  
  scene.setOnMousePressed(e -&gt; Toolkit.getDefaultToolkit().beep());
  </code></pre>
  
  <p>I also tried to put them in separate threads, it doesn't work either but if it does there is another problem, i cannot change the coordinates of a javafx component in another thread and i get this error -even if it doesn't cause an error it will not work:</p>
  
  <pre><code>java.lang.IllegalStateException: Not on FX application thread
  
  scene.setOnMouseMoved(e -&gt; {
          Thread thread = new Thread() {
              @Override
              public void run() {
                  imageView.setX(e.getX());  // here i cannot do stuff related
                  imageView.setY(e.getY());  // to javafx components
              }
          };
  
          thread.start();
      });
  
      scene.setOnMousePressed(e -&gt; {
          Thread thread = new Thread() {
              @Override
              public void run() {
                  Toolkit.getDefaultToolkit().beep());
              }
          };
  
          thread.start();
      });
  </code></pre>
  
  <p>I also tried this but it doesn't work either</p>
  
  <pre><code>scene.setOnMouseMoved(e -&gt; {
          imageView.setX(e.getX());
          imageView.setY(e.getY());
  
          scene.setOnMousePressed(event -&gt; Toolkit.getDefaultToolkit().beep());
      });
  </code></pre>
  
  <p>So how i can handle this problem, how i can respond to mouse clicks in parallel with mouse movements with no conflict.</p>