2017-07-30 71 views
0

我有一個小應用程序。有了這個應用程序,我可以添加一個新的員工記錄到MySQL數據庫,然後顯示這些記錄。 buildData()從數據庫中檢索數據並將其顯示在TableView中,每次添加新記錄以「刷新」TableView時,我也會調用此方法。然而,這並不像預期的那樣。JavaFX TableView爲相同的數據添加額外的列

這是被添加的第一個記錄後認爲:

enter image description here

,當我按下按鈕來添加另一條記錄,這是我得到:

enter image description here

如果我關閉了應用程序並重新打開它,TableView正常:

enter image description here

這是buildData()方法是什麼樣子

private ObservableList<ObservableList> data; 

public void buildData() { 
    Connection c; 
    TableColumn col; 
    data = FXCollections.observableArrayList(); 
    try { 

     c = DataBaseConnection.connectToDatabase(); 
     String SQL = "SELECT * FROM employees"; 

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

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

      final int j = i; 
      col = new TableColumn(rs.getMetaData().getColumnName(i + 1)); 
      col.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<ObservableList, String>, ObservableValue<String>>() { 
       public ObservableValue<String> call(TableColumn.CellDataFeatures<ObservableList, String> param) { 
        return new SimpleStringProperty(param.getValue().get(j).toString()); 
       } 
      }); 
      employeeTableView.getColumns().addAll(col); 
     } 
     while (rs.next()) { 

      ObservableList<String> row = FXCollections.observableArrayList(); 
      for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) { 
       row.add(rs.getString(i)); 
      } 
      data.add(row); 
     } 
     employeeTableView.setItems(data); 

    } catch (Exception e) { 
     e.printStackTrace(); 
     System.out.println("Error on Building Data"); 
    } 
} 

,然後方法到一個新的員工記錄添加到數據庫中(方法忽略類型檢查和所有that.Probably的原因是什麼?):

public void addEmployeeToDatabase() { 

    Employee employee = new Employee("Anne Droid", "Marketing", "Gender"); 

    String query = "INSERT INTO employees (Employee_Name, Department, Gender) VALUES (?, ? , ?)"; 

    try { 
     preparedStatement = connection.prepareStatement(query); 

     preparedStatement.setString(1, employee.getEmployeeName()); 
     preparedStatement.setString(2, employee.getDepartment()); 
     preparedStatement.setString(3, employee.getGender()); 
     preparedStatement.execute(); 

     buildData(); 
     connection.close(); 

    } catch (SQLException e) { 
     e.printStackTrace(); 
    } 
} 

最後,Employee類:使用PUR

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; 
} 
... 

上午e java,沒有FXML。我搜索了一個解決方案,但沒有可用(據我所知)。 任何指針?

回答

1

buildData方法中,您替換項目。但是,您可以在不刪除任何列的情況下向columns列表中添加新的TableColumn。這樣,每次調用buildData方法時,列數都會增加。

清除columns列表中添加新的列在解決這個問題之前:

... 

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

... 
+0

是啊!那樣做了。謝謝 –