0

I've created an application to draw objects on a JavaFX pane (rectangle, paths, custom controls etc..).

Now I want to be able to select those objects to move, copy or group them. The javafx Pane don't have a selectionModel by default and I somehow didn't find out how to implement such function.

If someone got an Idea on how to do this, I'll be grateful

Murdoc
  • 75
  • 3
  • 8

1 Answers1

1

You need to create your own selection model.

First you create a class SelectionModel. In that you put a Set. In order to put nodes into that set, you have to create a mouse handler that adds nodes to the model and removes them from the model depending on shift/ctrl pressed while you click on the nodes.

When you move the selection via mouse, you get the position of the currently clicked on node and instead of moving the single node in the event handler you move all nodes of the list in the SelectionModel.

In order to group them, you need to create a class/collection in which you can store the various nodes you selected. Usually the group is just a parent node. But that varies depending on your requirements.

Copy/Paste is a different matter. You need to create some kind of factory that creates and positions new nodes depending on the nodes in your selection model.

Here's an example with code for a start. It shows you how to select nodes.

Community
  • 1
  • 1
Roland
  • 18,114
  • 12
  • 62
  • 93
  • do you have an example on how to create/add a selection model to my controller? couldn't find any on the web – Murdoc Apr 19 '15 at 15:23
  • A selection model is simply a Set. There isn't much to it. Add your selected nodes to it or remove them. – Roland Apr 19 '15 at 16:04
  • are we talking about the same thing? https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/SelectionModel.html – Murdoc Apr 19 '15 at 16:10
  • I don't know if we are talking about the same thing, since you didn't provide a single line of code. – Roland Apr 19 '15 at 16:53
  • I wanted to know if you meant the javafx SelectionModel Class (linked)! to be honest, I don't know if it possible to post code for this issue, since I'm trying to understand how to implement it. please bear with me, I'm new to javaFx – Murdoc Apr 19 '15 at 17:07