2013-11-23 47 views
2

對於我來說,嘗試獲得答案非常困難。由於某種原因,Oracle不在其網站上記錄文件?它已經被問到在stackoverflow已經(JavaFX: TableView(fxml) filling with data,和JavaFX FXML table; why won't my data display),但是當我嘗試重新給出的答案,我仍然無法讓它工作。如果有人能夠提供幫助,我將非常感激!我會把所有的代碼放在一起。使用fxml使用javafx填充表格數據

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> 
+0

此外,當應用程序加載時,懸停在一條線上變成藍色,應該有字段,沒有任何事情發生。所以它似乎知道應該有這些行上的數據,但沒有顯示任何數據。不知道這是否有幫助。謝謝。 – user3023715

回答

1

的PropertyValueFactory試圖找到在兩個方面您的數據值:

1)尋找一個方法,例如nameProperty其中name是您在構造函數中指定的值,例如您的示例。 「RID」。

因此,你需要一個名爲像這樣的(現場+「地產」的名稱)方法:

public IntegerProperty rIDProperty() { 
    return rID; 
} 

併爲您的其他屬性類似。

2)如果這些不存在,它會查找名爲例如getName其中Name是您在構造函數中指定的,第一個字母大寫爲(遵循Java Beans約定)。

在你的例子中,你沒有1),但你似乎有2) - 但你沒有在getters和setter中大寫屬性名的第一個字母!

因此,將這些改爲例如

public Integer getRID() { 
    return rID.get(); 
} 

與此類似。

你需要1)或2),但是兩者都是好的做法。

另請注意,該問題與FXML無關。

+0

謝謝你的幫助! :) – user3023715