4

I want to programmatically add and remove side menu in BorderPane. Problem is when I add a Node, it is not visible. BorderPane and StackPane are defined in FXML file.

I want to do something like this:

StackPane temp = (StackPane) borderPane.getChildren().get(1);
borderPane.getChildren().remove(1);
borderPane.getChildren().add(0, temp);

I have tried borderPane.requestLayout() but it is not working.

DVarga
  • 21,311
  • 6
  • 55
  • 60
Milorad
  • 152
  • 2
  • 10

2 Answers2

7

You can use setRight or setLeft, setTop, setBottom, setCenter methods to add Nodes to different parts and also getRight, getLeft, getTop, getBottom, getCenter to retrieve the currently assigned Node. The set methods can be also used to remove the currently set Node by passing null value.

Example:

Imagine that you have a BorderPane that has a StackPane placed on the right side, and you want to move it to the left side.

StackPane temp = (StackPane) borderPane.getRight(); // Casting is unnecessary
borderPane.setRight(null);
borderPane.setLeft(temp);
Graham
  • 7,431
  • 18
  • 59
  • 84
DVarga
  • 21,311
  • 6
  • 55
  • 60
0

It's to simple to remove node from border pane. Declare border pane ID and using that id select children node and remove specified side(left,right,top,bottom) by below code.

    @FXML
    private BorderPane borderPane;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        borderPane.getChildren().remove(borderPane.getLeft());
        borderPane.getChildren().remove(borderPane.getRight());
    }