【问题标题】:Why is my table not being populated?为什么我的表没有被填充?
【发布时间】:2017-01-21 21:30:26
【问题描述】:

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

我填充数据库的方法:

    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>

【问题讨论】:

  • 我试过了(我也添加了TableColumns)并且它有效。你能发布完整的代码吗?您如何创建表格以及如何填充列?
  • 真的有效吗?我使用scenebuilder创建表。填充列是什么意思?我不认为我这样做了。 empInfoTable.getItems().addAll(empList) 不会填充列吗?
  • 是的。我在setCellValueFactory 调用之后添加了empInfoTable.getColumns().addAll(empIDColumn, fNameColumn, lNameColumn, gNameColumn, hoursColumn, payColumn); 之类的列,然后一切正常。这就是@Guenther 在他的回答中提出的。但是在您的情况下,因为您已经从 FXML 注入了表,所以问题是这一行 empInfoTable = new TableView&lt;&gt;();。在这里,您重新初始化 TableView(它已经由 FXMLLoader 初始化),因此您丢失了 FXML 文件中添加的列。删除此行,我想它无需任何额外修改即可工作。
  • 添加(至少)initialize() 方法。为什么要在 onSort 处理程序中填充表?

标签: java mysql javafx


【解决方案1】:

您的 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 ;

【讨论】:

    【解决方案2】:

    在您的方法 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?

    【讨论】:

    • 还是什么都没有。会不会是别的?
    • 如果你能看到正在服务器端打印的数据,但在视图上看不到输出(jsp、html),你能告诉我们这段代码吗?
    【解决方案3】:
    • 不要使用ListArrayList 而是创建ObservableArrayList 并将项目添加到其中,以便立即将更改反映到TableView:

      ObservableList<EmployeeMaster> data = FXCollections.observableArrayList( );
      table.setItems(data);
      
    • 请注意,方法EstimatedPayProperty() 必须是estimatedPayProperty()。同样在构造函数中使用camelCase 并使用(this)。

    • 建议tutorial遵循最佳实践

    • 您也可以在这里进行改进,例如:

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

      到:

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

    【讨论】:

    • 1) empInfoTable.getItems().addAll(empList); 将所有元素添加到存储在itemsProperty 中的ObservableList。 2) EstimatedPayProperty() 确实命名错误,但PropertyValueFactory 将寻找适当的getter 方法,以防缺少名称为getEstimatedPay() 的属性访问器方法存在。
    • @DVarga 修复了 1 :)。关于estimatedPayProperty 和getEstimatedPay(),当您实时编辑表格时,它会有所不同。第一个会立即在表格上反映更改。使用get() 不会立即反映更改。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多