• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

[JavaFX] Drag a node multiple times

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,

I'm trying to drag a rectangle to a VBox and then drag it again from inside the VBox. If the rectangle is dropped anywhere outside of the target VBox, then it should revert its position back to where it belonged: if it was a child of the left VBox, then reset it back inside of it; if it was a child of the right VBox, reset it back inside of the right VBox.
The first part works fine, I can drag it to the VBox and add it as a child. The second part is where I'm having a problem.



Steps to reproduce the issue:
1) Drag the left rectangle on top of the right one (inside the VBox)
2) Drag the newly added rectangle to a place outside of the VBox
3) Release the mouse button

The problem arises at step 2. What I'm expecting is that the VBox no longer gets any events from the mouse, since it left its boundary. This does not happen, as can be seen in the console output (the onMouseDragOver is still being called).
At step 3, an exception is thrown, but this happens because it tries to add the same rectangle back to the VBox.

What I'm expecting is that if I drag the rectangle outside of the right VBox and release the mouse, then the rectangle resets its position to be back inside the right VBox.

What I'm trying to figure out is how can it be that even though the cursor is not within the bounds of the VBox, it still receives MOUSE_DRAGGED events?

Thank you!
 
Sheriff
Posts: 6033
157
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, Smith Ron.

I looks like from your event handlers that you only are looking for dragging the disk from left to right.  I don't see any handling of dragging from the target back to the source.  Is this what you want to do?

My guess is that your target and source will both have to look for drag events and mouse release events and will need to differentiate the two.

A note about posting code that you might want others to execute: don't remove the include statements or the getters and setters.  It just makes it harder to run the program.  Code fragments are fine if they're not expected to be executed.
 
Smith Ron
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Knute Snortum,

Thank you very much for your useful feedback!
I have updated the post with the imports and the getters and setters. I have also updated the MOUSE_RELEASED event handler:

What I'm trying to see is that if I drag the left rectangle outside of its VBox (but not inside the right VBox), then when I release the mouse the position of the rectangle resets (goes back to its parent VBox). Also, if the left rectangle has been successfully added as a child of the right VBox, then the same scenario should apply to it: when I drag it outside of its VBox (but not inside any of the 2 VBoxes) and I release the mouse, then it should reset its position and go back to its parent VBox.
The problem that I'm seeing here is that, even though I leave the area of the right VBox when I'm dragging the disk outside of it, the VBox still receives events from the mouse, as if it didn't register that the cursor has left it.
Do you know what might be the issue here? Or perhaps there is a better approach to achieve this result?
Also, maybe it helps to know that what I'm trying to achieve is to create a Towers of Hanoi game simulation.

Thank you!
 
Knute Snortum
Sheriff
Posts: 6033
157
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've been playing with the program a little and I see your problem more clearly now.  You are right that the mouse events seem to disappear once you add the Rectangle to the VBox.  I am not sure how to "re-register" the Rectangle; I'll research this more.
 
Smith Ron
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much for your support!
I am also trying to figure this out, but so far I don't have any clues... I probably need more technical knowledge in order to debug this one, so I'll go and read more theory.
I think that if this issue can be solved, then I would make alot of progress with the application I'm working on, so I'd be really greatful if you could figure it out!
Thanks again!
 
Knute Snortum
Sheriff
Posts: 6033
157
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm getting the feeling that maybe adding the Rectangle to the VBox is not the way to go.  Maybe when you drag the Rectangle to an area, it should snap to a predefined position.  If you're creating a Towers of Hanoi game, all the rectangles should be present at once, correct?  Once a Rectangle's drag event detects that it's within an area and the mouse is released, move that Rectangle to a cented position centered in the area, but don't add it to a VBox or anything like that.  That area could just be another "hollow" Rectangle.  Use Node#toFront() to make sure the solid Rectangle is on top.
 
Smith Ron
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have tried to fix my problem but with no luck...
I will now start implementing your solution. Thanks!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!