javafx-无法将项目添加到Listview

时间:2016-12-06 07:17:34

标签: javafx

有我的fxml文件:

<AnchorPane fx:id="anch" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
    <children>
        <ListView fx:id="list" layoutX="200.0" layoutY="7.0" prefHeight="393.0" prefWidth="200.0" />
    </children>
</AnchorPane>

和controllerfile:

@FXML
private ListView<String> list;

@FXML
private AnchorPane anch;

ObservableList<String> li = FXCollections.observableArrayList("red","blue","yellow");

@Override
public void initialize(URL url, ResourceBundle rb) {
    // TODO
    list.setItems(li);

}

当我运行程序程序时,它只显示空列表。

1 个答案:

答案 0 :(得分:3)

在FXML中错过fx:controller

例如:

<AnchorPane id="AnchorPane" prefHeight="380.0" prefWidth="387.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="PackageName.ControllerFileName">

here's document from oracle