Javafx how to close an internal Anchorpane

Asked

Viewed 680 times

1

Hello folks my doubt is have a child Anchorpane that is added inside the parent Anchorpane in my son Anchorpane I have a button called close.

How do I close Anchorpane Son without closing Anchorpane Father?

my code is like this:

@FXML
private Button btnFacharAnchorPane;

@FXML
public void fachar() {
    btnFecharAnchorPane.getScene().getWindow().hide();
}

when I click the button it closes the whole program, what I want is that it closes only the internal Anchorpane that is the son

  • what’s going on there, is that both AnchorPane, belong to the same Stage; at the time of making btnFecharAnchorPane.getScene().getWindow().hide(); You’re calling the same Stage

  • then how do I close the inner anchorpane son without closing the parent anchorpane you know

2 answers

1


you can use the method setVisible() to hide and show the AnchorPane, in the button action fecharPane() should only add the following

        @FXML
        public void fecharPane(ActionEvent evt){
          filho.setVisible(false);
        }
  • this one, is one of the ways of what you are wanting to do, another would be, create a new Stage for this AnchorPane, in which case it will open another window.

  • thanks worked the way I wanted, more now you could explain to me what exactly the setVisible method() makes, you said it hides the anchorpane more and the one I opened the anchorpane a 30 times and in those 30 times I hide it I want to know the anchorpane will be close for real or the 30 times I opened it still is open more I’m not seeing if that’s the case that can cause a little slow in the program

  • the method setVisible(boolean value), it simply hides or shows the element, it will depend on the type value boolean, will not have problems close 30 or 40 times, it has already been loaded in memory, will only hide or show, another advantage of this method is that once it is false will not allow any action in the element.

  • thanks even now I understood thank you very much

0

To do this you must assign an ID to the parent Anchorpane and modify its method according to the code below:

@FXML
private AnchorPane pai;

// [...]

@FXML
public void fecharPane(ActionEvent event){
    // Remove do array de filhos o pane que contém o botão
    pai.getChildren().remove(mybutton.getParent());
}

Result: (Anchorpane father in blue and son in red)

Removendo AnchorPane filho

  • I tried to do more gave a mistake

  • In the code I made it clear that Anchorpane was the father, and in your code it was son ... Apparently you assigned the ID to the wrong panel.

  • I had put ID to Father Anchorpane but when I put the father code.getChildren(). remove(mybutton.getParent()); the name "father" was underlined in red

Browser other questions tagged

You are not signed in. Login or sign up in order to post.