Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaFX TableView为相同数据添加额外列_Java_Javafx_Javafx 2_Javafx Tableview - Fatal编程技术网

JavaFX TableView为相同数据添加额外列

JavaFX TableView为相同数据添加额外列,java,javafx,javafx-2,javafx-tableview,Java,Javafx,Javafx 2,Javafx Tableview,我有一个小应用程序。有了这个应用程序,我可以向MySQL数据库添加一个新的员工记录,然后显示这些记录buildData()从数据库检索数据并将其显示在TableView中,每次添加新记录以“刷新”TableView时,我也会调用此方法。然而,这并不像预期的那样 这是添加第一条记录后的视图: 当我点击按钮添加另一张唱片时,我得到的是: 如果关闭应用程序并重新打开,则TableView正常: 这就是buildData()方法的外观(该方法忽略了类型检查和所有这些。可能是原因?) 最后,Empl

我有一个小应用程序。有了这个应用程序,我可以向MySQL数据库添加一个新的员工记录,然后显示这些记录
buildData()
从数据库检索数据并将其显示在TableView中,每次添加新记录以“刷新”TableView时,我也会调用此方法。然而,这并不像预期的那样

这是添加第一条记录后的视图:

当我点击按钮添加另一张唱片时,我得到的是:

如果关闭应用程序并重新打开,则TableView正常:

这就是
buildData()
方法的外观(该方法忽略了类型检查和所有这些。可能是原因?)

最后,Employee类:

public class Employee {

private StringProperty employeeName;
private StringProperty department;
private StringProperty gender;

public Employee() {

}

public Employee(String employeeName, String department, String gender) {
    this.employeeName = new SimpleStringProperty(employeeName);
    this.department = new SimpleStringProperty(department);
    this.gender = new SimpleStringProperty(gender);
}

public String getEmployeeName() {
    return employeeName.get();
}

public StringProperty employeeNameProperty() {
    return employeeName;
}
...
我使用纯java,没有FXML。我已经搜索了一个解决方案,但没有可用的(据我所知)。
有指针吗?

buildData
方法中,您可以替换这些项。但是,您可以将新的
表格列
s添加到
列表中,而不删除任何列。这样,每次调用
buildData
方法时,列数都会增加

在添加新列之前,请清除
列表以解决此问题:

...

ResultSet rs = c.createStatement().executeQuery(SQL);

employeeTableView.getColumns().clear();

for (int i = 0; i < rs.getMetaData().getColumnCount(); i++) {

    final int j = i;
    col = new TableColumn(rs.getMetaData().getColumnName(i + 1));

    ...

    employeeTableView.getColumns().add(col);
}

...
。。。
ResultSet rs=c.createStatement().executeQuery(SQL);
employeeTableView.getColumns().clear();
对于(int i=0;i
是的!。成功了。非常感谢。
public class Employee {

private StringProperty employeeName;
private StringProperty department;
private StringProperty gender;

public Employee() {

}

public Employee(String employeeName, String department, String gender) {
    this.employeeName = new SimpleStringProperty(employeeName);
    this.department = new SimpleStringProperty(department);
    this.gender = new SimpleStringProperty(gender);
}

public String getEmployeeName() {
    return employeeName.get();
}

public StringProperty employeeNameProperty() {
    return employeeName;
}
...
...

ResultSet rs = c.createStatement().executeQuery(SQL);

employeeTableView.getColumns().clear();

for (int i = 0; i < rs.getMetaData().getColumnCount(); i++) {

    final int j = i;
    col = new TableColumn(rs.getMetaData().getColumnName(i + 1));

    ...

    employeeTableView.getColumns().add(col);
}

...