Hello! Here is the code

ScrollPane sp = new ScrollPane(); sp.setOnMousePressed(event -> { System.out.println("pressed"); }); sp.setOnMouseDragged(event -> { System.out.println("dragged"); }); 

But for some reason it does not work, nothing happens when you click on the ScrollPane. What's wrong?

  • one
    because setOnMousePressed accepts EventHandler, and you pass three points. - Victor
  • Describe your situation in more detail and show more code. On your two lines and vague "does not work" nothing can be understood. - m. vokhm
  • @Viktor if you are so calmer ... But I don’t think it will help you. - ProstoCoder
  • To click on sctollPane, at least you need to draw it on the screen, do it and try clicking. - Victor
  • @ Victor is hard to imagine, but before that I figured it out, but it still didn’t work - ProstoCoder

2 answers 2

Try the ananimous class option:

 ScrollPane sp = new ScrollPane(); sp.setOnMousePressed(new EventHandler() { public void handle (Event event) { System.out.println("X: " + ((MouseEvent) event).getX() + " Y: " + (MouseEvent) event).getY()); } }); 

Best of all, implement from the EventHandler interface and implement its handle method and write what you need in it, and when calling the sp.setOnMousePressed(this) method, just specify the this keyword to the constructor.

  • It just does not work, just nothing happens ... - ProstoCoder
  • @ProstoCoder means you are doing something wrong in your code, I am not a psychic to know what you want from your code. I suggest you edit your question in such a way that for those who are trying to read it and help, it is clear that you want to achieve your code. And once again, try all the same to implement the interface EventHandler - dev3java
  • Everything I do, you see, you just need to add a listener not to the scroll itself, but to the scroll content, thanks - ProstoCoder
  • @ProstoCoder Please post your answer if my not helped you. Thank. - dev3java
  • Here, if interested - ProstoCoder

Everything will work if you add listeners not to the ScrollPane itself, but to its content, i.e. here:

 ScrollPane sp = new ScrollPane(); Node content = sp.getContent(); content.setOnMousePressed(event -> { //Ваши действия }); 
  • Well, remember. Your code did not check, but I trust. Thanks for the answer for the public, I put +1 for the answer, and for the question I have already put +1. - dev3java