J'ai une petite application. Avec cette application, je peux ajouter un nouvel enregistrement employé à une base de données MySQL, puis afficher ces enregistrements. buildData()
récupère des données de la base de données et l'affiche dans TableView, j'appelle également cette méthode chaque fois qu'un nouvel enregistrement est ajouté pour "actualiser" le TableView. Cependant, cela ne se comporte pas comme prévu.JavaFX TableView ajoutant une colonne supplémentaire pour les mêmes données
est la vue après le premier enregistrement est ajouté:
quand je frappe le bouton pour ajouter un autre enregistrement, ce que je reçois:
Si Je ferme l'application et la rouvre, le TableView est normal:
C'est ce que la méthode buildData()
ressemble
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");
}
}
et la méthode pour ajouter un nouveau dossier de l'employé à la base de données (méthode ne tient pas compte TypeChecking et tous that.Probably la cause?):
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();
}
}
Enfin, la classe des employés:
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;
}
...
Am en utilisant pur e java, pas de FXML.J'ai cherché une solution, mais aucune n'est disponible (autant que je sache). Des pointeurs?
Eh oui !. C'est ce qu'il a fait. Je vous remercie –