Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
541 views
in Technique[技术] by (71.8m points)

java - MenuButton doesn't display items in Javafx

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

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

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...