2016-12-06 119 views
0

有我FXML文件:javafx-無法添加項目的ListView

<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); 

} 

當我運行該程序的程序,那隻能說明空列表。

回答

3

小姐fx:controller在FXML

例:

<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

+1

@manpreetsinghwadhoun如果答案爲你工作和似乎沒有成爲一個問題,質量或類似的東西,考慮接受答案。 – fabian

+0

是啊..工作 –