Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a class Foo which just load the FXML and create the scene.

In the FXML, I set the controller to be FooController (fx:controller="FooController")

And I add a MenuButton:

<MenuButton fx:id="menuButton" layoutX="264.1875" layoutY="146.5" mnemonicParsing="false" text="MenuButton" />

And I try to set the menuButton in the FooController:

public class FooController implements Initializable{
    @FXML
    final  MenuButton menuButton = new MenuButton("Modalities");
    @Override
    public void initialize(URL arg0, ResourceBundle arg1) {
        final ObservableList<CheckMenuItem> listFilter = FXCollections.observableArrayList();
        final  MenuButton menuButton = new MenuButton("Modalities");
        CheckMenuItem item1 = new CheckMenuItem("T1");
        CheckMenuItem item2 = new CheckMenuItem("T1C");
        CheckMenuItem item3 = new CheckMenuItem("T2");
        listFilter.addAll(item, item2, item3);
        menuButton.getItems().addAll(listFilter);
        menuButton.setId("menuButton");
    }
}

But despite of setting everything for the MenuButton it doesn't display any of the CheckMenuItems in the GUI.

How can I load those items in menuButton defined in the FXML?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
511 views
Welcome To Ask or Share your Answers For Others

1 Answer

Never set an @FXML initialized value to a new value.

In your posted code, you are doing this twice, when you should not be doing it at all.

The FXMLLoader will create new items within the hierarchy of the component it instantiates and inject references to these new items into your controller. If you then set these references to new values, the new values will never be included in the displayed component hierarchy unless you specifically add them there, which kind of defeats the purpose of using FXML in the first place as it ignores things defined in your FXML file.

What you should have is:

public class FooController {
    @FXML
    MenuButton menuButton;

    public void initialize() {
        menuButton.getItems().addAll(
            FXCollections.observableArrayList(
                new CheckMenuItem("T1"),
                new CheckMenuItem("T1C"),
                new CheckMenuItem("T2")
            )
        );
    }
}

Also note that if your CheckMenuItems in the above code are static rather than a dynamic list, then you could just define them all in your FXML document instead of creating them in code.

Note: This is a duplicate question and has been asked before (not the exact question, but the substance of it), but my google skills couldn't dig up some of the duplicates.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...