[英]Populating table data with javafx using fxml
對於我來說,很難找到答案。 Oracle由於某種原因沒有在其網站上記錄文檔? 已經有人問過stackoverflow( JavaFX:用數據填充TableView(fxml)和JavaFX FXML表;為什么我的數據不顯示 ),但是當我嘗試使用給出的答案重新創建時,我仍然無法獲取它上班。 如果有人能夠提供幫助,我將萬分感謝! 我將給出所有的代碼。
public class Heroes extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
try {
AnchorPane page = FXMLLoader.load(Heroes.class.getResource("FXMLDocument.fxml"));
Scene scene = new Scene(page);
primaryStage.setScene(scene);
primaryStage.setTitle("Sample Window");
primaryStage.setResizable(false);
primaryStage.show();
} catch (Exception e) {
}
}
}
public class FXMLDocumentController implements Initializable {
/**
* Initializes the controller class.
*/
@FXML
TableView<Table> tableID;
@FXML
TableColumn<Table, Integer> tID;
@FXML
TableColumn<Table, String> tName;
@FXML
TableColumn<Table, String> tDate;
@FXML
TableColumn<Table, String> tPrice;
int iNumber = 1;
ObservableList<Table> data =
FXCollections.observableArrayList(
new Table(iNumber++, "Superman", "12/02/2013", "20"),
new Table(iNumber++, "Batman", "2/02/2013", "40"),
new Table(iNumber++, "Aquaman", "1/02/2013", "100"),
new Table(iNumber++, "The Flash", "12/02/2013", "16"));
@Override
public void initialize(URL url, ResourceBundle rb) {
// System.out.println("called");
tID.setCellValueFactory(new PropertyValueFactory<Table, Integer>("rID"));
tName.setCellValueFactory(new PropertyValueFactory<Table, String>("rName"));
tDate.setCellValueFactory(new PropertyValueFactory<Table, String>("rDate"));
tPrice.setCellValueFactory(new PropertyValueFactory<Table, String>("rPrice"));
tableID.setItems(data);
}
}
public class Table {
SimpleIntegerProperty rID;
SimpleStringProperty rName;
SimpleStringProperty rDate;
SimpleStringProperty rPrice;
public Table(int rID, String rName, String rDate, String rPrice) {
this.rID = new SimpleIntegerProperty(rID);
this.rName = new SimpleStringProperty(rName);
this.rDate = new SimpleStringProperty(rDate);
this.rPrice = new SimpleStringProperty(rPrice);
System.out.println(rID);
System.out.println(rName);
System.out.println(rDate);
System.out.println(rPrice);
}
public Integer getrID() {
return rID.get();
}
public void setrID(Integer v) {
this.rID.set(v);
}
public String getrDate() {
return rDate.get();
}
public void setrDate(String d) {
this.rDate.set(d);
}
public String getrName() {
return rName.get();
}
public void setrName(String n) {
this.rDate.set(n);
}
public String getrPrice() {
return rPrice.get();
}
public void setrPrice(String p) {
this.rPrice.set(p);
}
}
<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml" fx:controller="heroes.FXMLDocumentController">
<children>
<SplitPane dividerPositions="0.535175879396985" focusTraversable="true" orientation="VERTICAL" prefHeight="400.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<items>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0">
<children>
<TableView fx:id="tableID" prefHeight="210.0" prefWidth="598.0" tableMenuButtonVisible="true" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<columns>
<TableColumn minWidth="20.0" prefWidth="40.0" text="ID" fx:id="tID" >
<cellValueFactory>
<PropertyValueFactory property="tID" />
</cellValueFactory>
</TableColumn>
<TableColumn prefWidth="100.0" text="Date" fx:id="tDate" >
<cellValueFactory>
<PropertyValueFactory property="tDate" />
</cellValueFactory>
</TableColumn>
<TableColumn prefWidth="200.0" text="Name" fx:id="tName" >
<cellValueFactory>
<PropertyValueFactory property="tName" />
</cellValueFactory>
</TableColumn>
<TableColumn prefWidth="75.0" text="Price" fx:id="tPrice" >
<cellValueFactory>
<PropertyValueFactory property="tPrice" />
</cellValueFactory>
</TableColumn>
</columns>
</TableView>
</children>
</AnchorPane>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0" />
</items>
</SplitPane>
</children>
</AnchorPane>
PropertyValueFactory嘗試通過兩種方式查找數據值:
1)尋找一個方法,例如nameProperty
,其中name
是您在構造函數中指定的name
,在您的示例中例如“ rID”。
因此,您需要這樣命名的方法(字段名稱+“屬性”):
public IntegerProperty rIDProperty() {
return rID;
}
和其他屬性類似。
2)如果不存在,則查找名為getName
,其中Name
是您在構造函數中指定的首字母大寫的名稱 (遵循Java Beans約定)。
在您的示例中,您沒有1),但您似乎有2)-但是您沒有在getter和setter中大寫屬性名稱的第一個字母!
所以將這些更改為
public Integer getRID() {
return rID.get();
}
和類似。
您需要1)或2),但同時擁有這兩個將是一個好習慣。
還要注意,該問題與FXML毫無關系。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.