0

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?

Pradeep
  • 1
  • 1
  • 3
  • Is there something wrong with `if (condition) vbox.setVisible(true);`? – FThompson Aug 24 '13 at 20:03
  • Hi,Thanks, I am not adding vbox from code (the class that extend Application). I have added it from scene builder, so I need to get hold of this particular vbox instance/ID somehow and then set its visibility I guess. The code you gave will work if I have added vbox to the scene from code, but I dont want to do that, just because adding it from scene builder is more easier. I also learnt that we could achieve this by using reflection and also learnt few points against it, so not sure as of now. – Pradeep Aug 24 '13 at 20:15
  • For better help sooner post an [SSCCE](http://www.sscce.org/) – Branislav Lazic Aug 24 '13 at 22:50
  • Hi, thanks. Not sure if this is the right way; public void start(Stage stage) throws Exception { Parent root = FXMLLoader.load(getClass().getResource("Window.fxml")); Scene scene = new Scene(root, 500, 500); stage.setScene(scene); stage.show(); //Now based on certain condition I would like to make vbox which I created in Window.fxml, visible to user // how do I do it? – Pradeep Aug 25 '13 at 07:46
  • Sorry, I could not add the code in the right format – Pradeep Aug 25 '13 at 07:46
  • After lot of searching, [I found the answer here,][1] [1]: http://stackoverflow.com/questions/12324799/javafx-2-0-fxml-strange-lookup-behaviour Thank you all for your responses. – Pradeep Oct 06 '13 at 18:18

1 Answers1

1

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();
none_
  • 535
  • 4
  • 9