Jason Per
Jason Per

Reputation: 149

JavaFX hover property

I have a GridPane which is populated by rectangles. What I want to do is to display a new pane while hovering over a particular rectangle(member of the grid pane). Lets have an example with this VBox code example below. How could I make it display on hover ?

    Rectangle r = new Rectangle(RECTANGLE_SIZE, RECTANGLE_SIZE);
            r.hoverProperty().addListener((observable) -> {
                r.setFill(Color.BLACK);
                VBox box = new VBox();
                Button x = new Button("Test");
                box.getChildren().add(x);
            });

the set fill works properly

Upvotes: 0

Views: 3396

Answers (1)

MBec
MBec

Reputation: 2210

In your example you need to specify Node which would be parent of VBox. For now you are always creating new VBox, but never add it to current scene graph. Try this:

r.hoverProperty().addListener((ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean show) -> {
      if (show) {
           VBox box = new VBox();
           Button x = new Button("Test");
           box.getChildren().add(x);
           parent.getChildren().add(box);
      } else {
           parent.getChildren().clear();
      }
});

Upvotes: 1

Related Questions