无法将值插入到tableview中 - JavaFx

问题描述:

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

在此先感谢。

感谢@Uluk BIY。需要无论他提到,要改变了,我发现我错过了<TableColumn>标签在FXML文件,该文件是如下各列下<cellValueFactory><PropertyValueFactory>标签,

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

与此类似,所有列需要要这样更新。

在此更改后,代码工作正常。我可以将这些值添加到行中。 :)

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