Reputation: 20555
I have a Hbox that contains a lot of rectangles. When i press the one of the Nodes in the Hbox i want it to tell me what position that rectangle is in the observablelist how can i achieve this?
the following is an attempt (however it did not work)
figureRowBox.setOnMousePressed(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
for (Node n : figureRowBox.getChildren()) {
if (n.isPressed()) {
System.out.println(figureRowBox.getChildren().indexOf(n));
}
}
}
});
Where figureRowBox is a Hbox
Upvotes: 3
Views: 243
Reputation: 159406
Here is a hit test routine I use to determine which node was pressed in a HBox:
final HBox images = new HBox(10);
...
imageView.setOnMousePressed(new EventHandler<MouseEvent>() {
@Override public void handle(MouseEvent mouseEvent) {
final Object selectedNode = mouseEvent.getSource();
final int selectedIdx = images.getChildren().indexOf(selectedNode);
label.setText(
"Selected Vehicle: " + (selectedIdx + 1)
);
}
});
Here is a link to complete sample code:
Upvotes: 2