简体   繁体   中英

How to access the vbox that I created via javafx scene builder?

I have added a vbox from scene builder and set its visibility to false. Based on certain condition I want to set visibility to true. How do I do it?

I am new to java as well, so I am not able to sort out the problem with root.getChildren() which throws a compilation error, getChildren() has protected access in Parent. Could you please help?

It takes three steps.

  1. Define controller Java class in your FXML file. Either in SceneBuilder or directly in XML.
  2. In controller define field VBox and annotate it with @FXML and its name use in XML like fx:id . This will tell to JavaFX to bind your field with correct instance of VBox.
  3. Do whatever you want with VBox

XML definition (notice fx:controller and fx:id ):

    <BorderPane prefHeight="600.0" prefWidth="1024.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="foo.bar.MainController">
      <center>
        <VBox fx:id="content" fillWidth="true" prefHeight="200.0" prefWidth="100.0" />
      ...

Controller class:

    public class MainController {
          @FXML
          private VBox content;
    }

If you want to call controller class from outside, you will get instance of controller from right instance of FXMLLoader like this:

   MainController controller = (MainController) loader.getController();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM