简体   繁体   English

Java ComboBox将不会被填充

[英]Java ComboBox will not get populated

I have tried everything to figure out why my combobox will not get populated but nothing works. 我已经尽一切努力弄清楚为什么我的组合框不会被填充但什么都没用。

In my FXML file I have. 在我的FXML文件中。

<ComboBox fx:id="comboBox" layoutX="162.0" layoutY="15.0" prefHeight="25.0" prefWidth="334.0" promptText="Select past popular pizza" />

In my controller I have the following 在我的控制器中,我有以下内容

public class ServeController {
public ObservableList<String> pizzas1 = FXCollections.observableArrayList();
@FXML public ComboBox<String> comboBox;
private void initialize() {
comboBox.getItems().addAll(
        "jacob.smith@example.com",
        "isabella.johnson@example.com",
        "ethan.williams@example.com",
        "emma.jones@example.com",
        "michael.brown@example.com"  
    );
}

I have even tried with 我什至尝试过

public ObservableList<String> pizzas1 = FXCollections.observableArrayList("1","2","3","4");

to try to get some kind of data to show up but nothing seems to be working. 尝试显示某种数据,但似乎没有任何效果。

I assume that you are initalizing controller in "standard" way. 我假设您以“标准”方式初始化控制器。 I'm guesing that private void initialize() method is never invoked. 我猜测永远不会调用private void initialize()方法。 Your ServeController does not implement Initializable interface. 您的ServeController没有实现Initializable接口。 Correct code should look as following: 正确的代码应如下所示:

public class ServeController implements Initializable {

    public ObservableList<String> pizzas1 = FXCollections.observableArrayList();

    @FXML
    public ComboBox<String> comboBox;

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        comboBox.getItems().addAll(
                "jacob.smith@example.com",
                "isabella.johnson@example.com",
                "ethan.williams@example.com",
                "emma.jones@example.com",
                "michael.brown@example.com"
        );
    }
}

I assume that if you add a printout to initialize() you'll see that it is not invoked. 我假设如果将打印输出添加到initialize() ,则会看到未调用它。
To get initialize() invoked you need to annotate it: @FXML private void initialize() 要调用initialize() ,需要对其进行注释: @FXML private void initialize()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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