java - 为什么我的表没有被填充?

标签 java mysql javafx

我知道那里有数据,因为当我遍历列表时,我可以看到我所有的数据都在那里。即使只是打印我的数据似乎也不错,但我只是不知道为什么它似乎无法显示出来

我填充数据库的方法:

    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>

最佳答案

您的 populateDatabase() 方法创建一个新的 TableView 并填充它,而不是填充您在 FXML 文件中定义的那个。

删除行

empInfoTable = new TableView<>();

并以与处理列等相同的方式将表注入(inject) Controller (注意 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 ;

关于java - 为什么我的表没有被填充?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39484278/

相关文章:

Mysql如何按最高计数列从最低排序

php - mySQL:在结果内的间隔之间自动插入行

java - 为什么不能使用 Void 作为 main 方法的返回类型

java - 是否可以将不同的子类传递给同一个 PCollection?

Mysql 进程 CPU 使用率超过 3500%,Qcache_lowmem_prunes 过高

java - 如何将事件处理程序附加到 JavaFX Stage/Window Minimize Button?

ffmpeg - 用于在内存中将 MP4 转换为 MPEG-2 TS 的 Java 库?

java - 如何通过浏览器下载可执行jar?

java - 相关组合框

JavaFX TableColumn 图形不隐藏