Draggable Node in Javafx 2.0

Hello I’ve been working on javafx 2.0. JavaFX has very flexible and customizable controls. The JavaFX is specially for GUI purpose only . So Today I would like to show how to make a Draggable Node in Javafx 2.0.

First Let’s make one customNode class which extends from the Parent. Ok now let’s see the class .

I think the above code is quite simple for the java programmer. I actually used four variable for saving x and y position. The x and y position of Mouse and other x and y position of Node.

Now let’s see the use of Draggable Node .

Css “main.css”

Ok now that’s it. Just run the program and you will see the Rectangle with 50px right and it’s draggable too.
If you like to see the demo then see at here: Draggable Node

*But remember that if you use the controls inside children of customNode then the draggable won’t work at all. Because the focus is taken by the Controls

Please don’t feel hesitate on commenting . Have a good day and Thanks for viewing blog. 🙂

20 thoughts on “Draggable Node in Javafx 2.0”

  1. Small problem in Netbeans. Had to change
    “onMouseDraggedProperty().set(new EventHandler(){”
    to
    “onMouseDraggedProperty().set(new EventHandler(){”
    Otherwise it would complain.

  2. I have a similar problem in that Netbeans complains that you have not implemented all the abstract methods (the handle function)

    it requires it to be passed an Event and in your code it passes a MouseEvent

    how did you get that to compile. I was thinking of using the handle event and casting the Event to a MouseEvent to get it to work here…

    And thanks for helping me at Oracle forums also – good blogging!

  3. As the comments didnt help me finding the problem i post the solution here that everyone can use it

    You have to change:
    onMousePressedProperty().set(new EventHandler(){
    To
    onMousePressedProperty().set(new EventHandler(){

    greetings, Martin

  4. Narayan Gopal Maharjan

    Sorry for the inconvience. Actually I should have used new EventHandler instead only newEventHandler . I’ve edited , Now it works fine.

  5. Pingback: Where is ‘blockinMouse’ Property in Javafx 2.0 ! « Java and FX

  6. it is even more simple in javafx with the setOnMouseDragged method
    you don’t need to create a class for that

    rectangle.setOnMouseDragged(new EventHandler()
    {
    public void handle(MouseEvent me)
    {
    rectangle.setCenterX(me.getSceneX());
    rectangle.setCenterY(me.getSceneY());
    }
    }

    when you already knew about this and created that class for another situation or purpose
    im sorry for bothering

  7. I extended your example and put the panel on a pane and then set the pane to be the content of a scroll pane. However, I can’t find out how to make the scrollbars appear when I drag the rectangle out of the scene / scrollpane / pane.

    Any hint is highly appreciated, thanx.

  8. ya it doesn’t work for the controls because the controls already takes first focus but there is hack to make these things working.
    You can either use :
    1. set setMouseTransparent(true); while your control is in dragging mode and again retain them to setMouseTransparent(false); when your dragging mode is off.
    2. using some sort of blockinMouseProperty hack

  9. Hiiii Narayan
    how to create slider window in javafx like we are doing in modalbox when we click on modalbox it will come with sliding effect please send me the code for that slider window please give me reply back as soon as possible ([email protected]) if possible then send me the code on my mail
    Thank you so much

  10. Hiiii Narayan
    how to create slider window in javafx like we are doing in modalbox when we click on modalbox it will come with sliding effect please send me the code for that slider window please give me reply back as soon as possible ([email protected]) if possible then send me the code on my mail
    Thank you so much

  11. Hi Farhan,
    Sliding a window is just a the Transition of a Node I guess. Are you trying to make the use of sliding window like of ANDROID/IOS system? If so then you can make use of KeyFrames, KeyValue and Animation or may be you can try some Transitions of JavaFX.

    Thanks

Leave a Reply