@Override public void start(Stage primaryStage) { Rectangle rect = new Rectangle(100, 100, Color.LIME); VBox root = new VBox(new Rectangle(100, 100, Color.RED), rect, new Rectangle(100, 100, Color.BLUE)); Scene scene = new Scene(root, 200, 300); scene.setOnMouseClicked(evt -> { rect.setManaged(!rect.isManaged()); rect.setVisible(!rect.isVisible()); }); primaryStage.setScene(scene); primaryStage.show(); }
rect.setManaged(false);
/** * Styles the selected and unselected area. */ private void styleAreas() { selectedArea.fillProperty().bind(getSkinnable().selectionAreaFillProperty()); selectedArea.strokeProperty().bind(getSkinnable().selectionBorderPaintProperty()); selectedArea.strokeWidthProperty().bind(getSkinnable().selectionBorderWidthProperty()); selectedArea.setStrokeType(StrokeType.OUTSIDE); // if the control's layout depends on this rectangle, // the stroke's width messes up the layout if the selection is on the pane's edge selectedArea.setManaged(false); selectedArea.setMouseTransparent(true); unselectedArea.setFill(Color.TRANSPARENT); unselectedArea.strokeProperty().bind(getSkinnable().unselectedAreaFillProperty()); unselectedArea.strokeWidthProperty().bind( Bindings.max(getSkinnable().widthProperty(), getSkinnable().heightProperty())); unselectedArea.setStrokeType(StrokeType.OUTSIDE); // this call is crucial! it prevents the enormous unselected area from messing up the layout unselectedArea.setManaged(false); unselectedArea.setMouseTransparent(true); }
/** * Creates the node which will be used to capture mouse events. Events are handed over to * {@link #handleMouseEvent(MouseEvent) handleMouseEvent}. * * @return a {@link Node} */ private Node createMouseNode() { Rectangle mouseNode = new Rectangle(); // make the node transparent and make sure its size does not affect the control's size mouseNode.setFill(Color.TRANSPARENT); mouseNode.setManaged(false); // bind width and height to the control mouseNode.widthProperty().bind(getSkinnable().widthProperty()); mouseNode.heightProperty().bind(getSkinnable().heightProperty()); // let it handle the mouse events if allowed by the user mouseNode.addEventHandler(MouseEvent.ANY, this::handleMouseEvent); mouseNode.mouseTransparentProperty().bind(getSkinnable().selectionMouseTransparentProperty()); return mouseNode; }