2016-09-14 93 views
4

我知道那裏有數據,因爲當我遍歷列表時,我可以看到我的所有數據都在那裏。即使只是打印我的數據看起來不錯,但我只是不知道爲什麼它只是不能似乎讓它顯示出來爲什麼我的表沒有被填充?

我用於填充數據庫的方法:

protected void populateDatabase() throws SQLException { 
     empInfoTable = new TableView<>(); 



     empIDColumn.setCellValueFactory(new PropertyValueFactory<EmployeeMaster, String>("employeeID")); 
     fNameColumn.setCellValueFactory(new PropertyValueFactory<EmployeeMaster, String>("firstName")); 
     lNameColumn.setCellValueFactory(new PropertyValueFactory<EmployeeMaster, String>("lastName")); 
     gNameColumn.setCellValueFactory(new PropertyValueFactory<EmployeeMaster, String>("groupName")); 
     hoursColumn.setCellValueFactory(new PropertyValueFactory<EmployeeMaster, String>("estimatedHours")); 
     payColumn.setCellValueFactory(new PropertyValueFactory<EmployeeMaster, String>("estimatedPay")); 



     List<EmployeeMaster> empList = getEmpFromDatabase(); 
     Iterator<EmployeeMaster> iterator = empList.iterator(); 
     System.out.print(iterator.next()); 

     empInfoTable.getItems().addAll(empList); 
     //System.out.print(empInfoTable.getItems()); 

    } 

方法從數據庫中抓取數據

private List<EmployeeMaster> getEmpFromDatabase() throws SQLException { 
     List<EmployeeMaster> row = new ArrayList<EmployeeMaster>(); 
     Connection connect = DriverManager.getConnection(
       "jdbc:mysql://localhost:3306/AUStudentEmployees", "root", 
       "password"); 

     Statement stmnt = connect.createStatement(); 
     ResultSet result = stmnt.executeQuery("select * from emp_info ORDER by EmployeeID"); 
      while(result.next()){ 

      String empID = result.getString("EmployeeID"); 
      String fName = result.getString("FirstName"); 
      String lName = result.getString("LastName"); 
      String gName = result.getString("GroupName"); 
      String hours = result.getString("EstimatedHours"); 
      String pay = result.getString("EstimatedPay"); 

      EmployeeMaster empInfo = new EmployeeMaster(empID, fName, lName, gName, hours, pay); 
      row.add(empInfo); 
      //empInfoTable.getItems().addAll(row); 
      //System.out.print(row); 
     } 
     return row; 
    } 

類的所有字符串屬性:

import javafx.beans.property.SimpleStringProperty; 
import javafx.beans.property.StringProperty; 

public class EmployeeMaster { 

    public EmployeeMaster(){ 

    } 

    private final StringProperty employeeID = new SimpleStringProperty(this, "EmployeeID"); 
    private final StringProperty firstName = new SimpleStringProperty(this, "FirstName"); 
    private final StringProperty lastName = new SimpleStringProperty(this, "LastName"); 
    private final StringProperty groupName = new SimpleStringProperty(this, "GroupName"); 
    private final StringProperty estimatedHours = new SimpleStringProperty(this, "EstimatedHours"); 
    private final StringProperty estimatedPay = new SimpleStringProperty(this, "EstimatedPay"); 

    public StringProperty employeeIDProperty(){ 
     return employeeID; 
    } 
    public StringProperty firstNameProperty(){ 
     return firstName; 
    } 
    public StringProperty lastNameProperty(){ 
     return lastName; 
    } 
    public StringProperty groupNameProperty(){ 
     return groupName; 
    } 
    public StringProperty estimatedHoursProperty(){ 
     return estimatedHours; 
    } 
    public StringProperty EstimatedPayProperty(){ 
     return estimatedPay; 
    } 

    public final String getEmployeeID() { 
     return employeeIDProperty().get(); 
    } 
    public final String getFirstName() { 
     return firstNameProperty().get(); 
    } 
    public final String getLastName() { 
     return lastNameProperty().get(); 
    } 
    public final String getGroupName() { 
     return groupNameProperty().get(); 
    } 
    public final String getEstimatedHours() { 
     return estimatedHoursProperty().get(); 
    } 
    public final String getEstimatedPay() { 
     return EstimatedPayProperty().get(); 
    } 

    public final void setEmployeeID(String EmployeeID){ 
     employeeIDProperty().set(EmployeeID); 
    } 
    public final void setFirstName(String fName){ 
     firstNameProperty().set(fName); 
    } 
    public final void setLastName(String lName){ 
     lastNameProperty().set(lName); 
    } 
    public final void setGroupName(String gName){ 
     groupNameProperty().set(gName); 
    } 
    public final void setEstimatedHours(String EstimatedHours){ 
     estimatedHoursProperty().set(EstimatedHours); 
    } 
    public final void setEstimatedPay(String EstimatedPay){ 
     EstimatedPayProperty().set(EstimatedPay); 
    } 

    public EmployeeMaster(String EmployeeID, String fName, String lName, String gName, 
      String EstimatedHours, String EstimatedPay){ 

     setEmployeeID(EmployeeID); 
     setFirstName(fName); 
     setLastName(lName); 
     setGroupName(gName); 
     setEstimatedHours(EstimatedHours); 
     setEstimatedPay(EstimatedPay); 
    } 

} 

編輯:FXML文件

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

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

<AnchorPane prefHeight="300.0" prefWidth="650.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="studentempsignin.Admin_Controller"> 
    <children> 
     <TableView layoutX="25.0" layoutY="29.0" onSort="#populateDatabase" prefHeight="166.0" prefWidth="600.0"> 
     <columns> 
      <TableColumn fx:id="empIDColumn" prefWidth="100.0" text="Employee ID" /> 
      <TableColumn fx:id="fNameColumn" prefWidth="100.0" text="First Name" /> 
      <TableColumn fx:id="lNameColumn" prefWidth="100.0" text="Last Name" /> 
      <TableColumn fx:id="gNameColumn" prefWidth="100.0" text="Group" /> 
      <TableColumn fx:id="hoursColumn" prefWidth="100.0" text="Hours" /> 
      <TableColumn fx:id="payColumn" prefWidth="100.0" text="Pay" /> 
     </columns> 
     </TableView> 
    </children> 
</AnchorPane> 
+0

我試了一下(我也添加了'TableColumn's),它的工作原理。你能發佈完整的代碼嗎?你如何創建表格以及如何填充列? – DVarga

+0

它真的有用嗎?我使用scenebuilder來創建表格。你填什麼是什麼意思?我不認爲我做到了。 empInfoTable.getItems().addAll(empList)不會填充列嗎? –

+1

是的。我添加了像empInfoTable.getColumns().addAll(empIDColumn,fNameColumn,lNameColumn,gNameColumn,hoursColumn,payColumn)這樣的列;'在setCellValueFactory'調用之後,一切正常。這是@Guenther在他的回答中提出的。但在你的情況下,你已經從FXML注入了表,問題是這行'empInfoTable = new TableView <>();'。在這裏,你重新初始化了'TableView'(它已經被'FXMLLoader'初始化了),所以你失去了添加在FXML文件中的列。刪除這一行,我想這將不會做任何額外的修改。 – DVarga

回答

0

在您的方法populateDatabase()中,您正在創建一個新的TableView對象。您需要將TableColumn對象添加到此新創建的TableView對象,例如像這樣

empInfoTable.getColumns().add(empIDColumn); 

與其他列相似。

編輯1 除此之外,屬性的名稱與您在EmployeeMaster中的名稱不匹配。

new PropertyValueFactory<EmployeeMaster, String>("employeeID") 

將查找名爲'employeeIDProperty'的屬性。請參閱https://docs.oracle.com/javafx/2/api/javafx/scene/control/cell/PropertyValueFactory.html或這裏How to use the PropertyValueFactory correctly?

+0

什麼都沒有。它可能是別的嗎? –

+0

@BobB我編輯了我的答案。 – Guenther

+0

如果您能夠看到正在服務器端打印的數據,並且無法看到視圖(jsp,html)上的輸出,您能向我們展示該代碼嗎? –

0
  • 而不是使用ListArrayList的做出ObservableArrayList,並添加項目到會這樣的變化會立即反映到的TableView:

    ObservableList<EmployeeMaster> data = FXCollections.observableArrayList(); 
    table.setItems(data); 
    
  • 注意,方法EstimatedPayProperty()必須是estimatedPayProperty()。另外在構造函數中使用camelCase和play(this。)

  • 推薦這個tutorial

  • 遵循最佳實踐您也可以在這裏有一個改進,例如:

    empIDColumn.setCellValueFactory(new PropertyValueFactory<EmployeeMaster, String>("employeeID")); 
    

    到:

    empIDColumn.setCellValueFactory(new PropertyValueFactory<>("employeeID")); 
    
+0

1)'empInfoTable.getItems().addAll(empList);'將所有元素添加到'itemsProperty'中存儲的'ObservableList'中。 2)'EstimatedPayProperty()'實際上被命名爲錯誤的,但'PropertyValueFactory'將在缺少名爲getEstimatedPay()的屬性存取方法的情況下尋找合適的getter方法。 – DVarga

+0

@DVarga修正了1 :)。關於estimatedPayProperty和getEstimatedPay(),當您實時編輯表格時,它會有所不同。第一個反映了表格中立即發生的變化。使用get(),變化並未立即反映出來。 – GOXR3PLUS

2

populateDatabase()方法創建一個新的TableView並填充它,而不是填充您在FXML文件中定義的那個。

刪除線

empInfoTable = new TableView<>(); 

,並注入該表到您的列,等做同樣的方式,控制器(注意fx:id):

<TableView fx:id="empInfoTable" layoutX="25.0" layoutY="29.0" onSort="#populateDatabase" prefHeight="166.0" prefWidth="600.0"> 

@FXML 
private TableView<EmployeeMaster> empInfoTable ; 
相關問題