0

I have a class called MapCanvas that extends Canvas from the JavaFX library. On this canvas, I'd like to create a method that allows a button to be placed on the canvas itself (instead of a pane on top), such that when I pan and zoom in on the canvas, the button keeps the coordinates that were specified when created (so it moves around when I drag the canvas).

The method would look like this:

public void addButton(Button button) {
    // add the button to the canvas
}

So my question is: Is it possible to add a method like this, and if so, how?

  • 2
    `Canvas` is not a subtype of `Parent`. Therefore the conventional means of adding a `Button` don't work. You could of course draw a screenshot of the button on the canvas, but making it responsive (hover/pressed effects, event handlers) would be a lot of trouble. I recommend wrapping both the `Canvas` and the `Button` in a suitable parent (e.g. a `AnchorPane` with all constraints for the `Canvas` set to `0d`) and apply any transformations to this parent instead of the canvas itself... – fabian Mar 19 '19 at 10:50
  • 1
    The first example adduced [here](https://stackoverflow.com/a/43814198/230513) adds a `CheckBox` to the `Parent` enclosing a `Pane` that contains a `Canvas`. – trashgod Mar 19 '19 at 16:33

0 Answers0