2013-02-14 63 views
1

我是JavaFx的新手,我嘗試創建一個tableview,並使用場景構建器創建fxml。如果我運行該程序,表格沒有獲取值。我發現這個問題在某種程度上與我的要求(javaFX 2.2 - Not able to populate table from Controller)相匹配,但我的代碼也與此相匹配。但我仍然無法獲得表格中的值。無法將值插入到tableview中 - JavaFx

我有參考以下視頻:http://www.youtube.com/watch?v=HiZ-glk9_LE

我的代碼如下,

MainView.java

/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package controller; 

import javafx.application.Application; 
import javafx.fxml.FXMLLoader; 
import javafx.scene.Scene; 
import javafx.scene.layout.AnchorPane; 
import javafx.stage.Stage; 

public class MainView extends Application { 

    public static void main(String[] args) { 
     launch(args); 
    } 

    @Override 
    public void start(Stage primaryStage) throws Exception { 
     try { 
      AnchorPane page = FXMLLoader.load(MainView.class.getResource("MainView.fxml")); 
      Scene scene = new Scene(page); 
      primaryStage.setScene(scene); 
      primaryStage.setTitle("Sample Window"); 
      primaryStage.setResizable(false); 
      primaryStage.show(); 
     } catch (Exception e) { 
     } 
    } 
} 

MainView.fxml

<?xml version="1.0" encoding="UTF-8"?> 

<?import java.lang.*?> 
<?import java.util.*?> 
<?import javafx.scene.*?> 
<?import javafx.scene.control.*?> 
<?import javafx.scene.layout.*?> 

<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml" fx:controller="controller.MainViewController"> 
    <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" /> 
       <TableColumn prefWidth="100.0" text="Date" fx:id="tDate" /> 
       <TableColumn prefWidth="200.0" text="Name" fx:id="tName" /> 
       <TableColumn prefWidth="75.0" text="Price" fx:id="tPrice" /> 
       </columns> 
      </TableView> 
      </children> 
     </AnchorPane> 
     <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0" /> 
     </items> 
    </SplitPane> 
    </children> 
</AnchorPane> 

MainViewController.java

/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package controller; 

import java.net.URL; 
import java.util.ResourceBundle; 
import javafx.collections.FXCollections; 
import javafx.collections.ObservableList; 
import javafx.fxml.FXML; 
import javafx.fxml.Initializable; 
import javafx.scene.control.TableColumn; 
import javafx.scene.control.TableView; 
import javafx.scene.control.cell.PropertyValueFactory; 
import model.Table; 

/** 
* FXML Controller class 
* 
*/ 
public class MainViewController 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++, "Dinesh", "12/02/2013", "20"), 
      new Table(iNumber++, "Vignesh", "2/02/2013", "40"), 
      new Table(iNumber++, "Satheesh", "1/02/2013", "100"), 
      new Table(iNumber++, "Dinesh", "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); 

    } 
} 

Table.java

/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package model; 

import javafx.beans.property.SimpleIntegerProperty; 
import javafx.beans.property.SimpleStringProperty; 


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

在此先感謝。

回答

0

感謝@Uluk BIY。需要無論他提到,要改變了,我發現我錯過了<TableColumn>標籤在FXML文件,該文件是如下各列下<cellValueFactory><PropertyValueFactory>標籤,

<TableColumn minWidth="20.0" prefWidth="40.0" text="ID" fx:id="tID" > 
<cellValueFactory> 
    <PropertyValueFactory property="tID" /> 
    </cellValueFactory> 
</TableColumn> 

與此類似,所有列需要要這樣更新。

在此更改後,代碼工作正常。我可以將這些值添加到行中。 :)

0

在Table.java改變所有getter和setter的名字,
變化GETR *至* GETR
變化SETR *至* SETR