我正在开发简单的JavaFX应用程序。在我的主视图中,我有Table View。在行选择方法中调用selectedEmployeeDetails()。它填充标签,显示员工的详细信息(姓名,姓名等)。当我启动我的应用程序并选择第一行(仅第一行)时,没有任何事情发生。当我选择secound行时一切都很好。然后当我再次选择第一行时,每个工作正常。这是我在主视图控制器的初始化方法中的代码。
@FXML
private void initialize() {
dialog.setMaximized();
dialog.setTitle("Test");
this.employeeModel.loadData();
employees = this.employeeModel.getEmployees();
employeeTableView.setItems(employees);
this.employeeFirstName.setCellValueFactory(
new PropertyValueFactory<Employee, String>("name"));
this.employeeFirstName.setCellFactory(TextFieldTableCell.forTableColumn());
this.employeeLastName.setCellValueFactory(
new PropertyValueFactory<Employee, String>("lastname"));
this.employeeLastName.setCellFactory(TextFieldTableCell.forTableColumn());
this.employeeFatherClmn.setCellValueFactory(
new PropertyValueFactory<Employee, String>("fathersName"));
this.employeeFatherClmn.setCellFactory(TextFieldTableCell.forTableColumn());
this.employeeJmbgClmn.setCellValueFactory(
new PropertyValueFactory<Employee, String>("jmbg"));
this.employeeJmbgClmn.setCellFactory(TextFieldTableCell.forTableColumn());
this.employeeContractClmn.setCellValueFactory(
new PropertyValueFactory<Employee, String>("contractType"));
this.employeeContractClmn.setCellFactory(TextFieldTableCell.forTableColumn());
this.employeePositionClmn.setCellValueFactory(
new PropertyValueFactory<Employee, String>("position"));
this.employeePositionClmn.setCellFactory(TextFieldTableCell.forTableColumn());
this.employeeStatusClmn.setCellValueFactory(
new PropertyValueFactory<Employee, String>("status"));
this.employeeStatusClmn.setCellFactory(TextFieldTableCell.forTableColumn());
//employeeTableView.getColumns().addAll(employeeFirstName, employeeLastName);
selectedEmployeeDetails(null);
//Add change listener
employeeTableView.getSelectionModel().selectedItemProperty().addListener((observableValue, oldValue, newValue) -> {
//Check whether item is selected and set value of selected item to Label
if (employeeTableView.getSelectionModel().getSelectedItem() != null) {
selectedEmployeeDetails(newValue);
System.out.println(employeeTableView.getSelectionModel().getSelectedIndex());
}
else
{
System.out.println("EROR");
}
});
}
有人可以帮我看看我的第一行不会在第一次选择时触发selectedEmployeeDetails()方法的主要原因是什么。
修改
private void selectedEmployeeDetails(Employee employee)
{
if(employee != null)
{
this.employeeDetailLastname.setText(employee.getLastname());
this.emloyeeDetailFathersName.setText(employee.getFathersName());
this.employeeDetailName.setText(employee.getName());
this.emloyeeDetailJmbg.setText(employee.getJmbg());
this.emloyeeDetailPosition.setText(employee.getPosition());
this.employeeDetailContractType.setText(employee.getContractType());
this.employeeDetailStatus.setText(employee.getStatus());
}
else
{
this.employeeDetailLastname.setText("");
this.emloyeeDetailFathersName.setText("");
this.employeeDetailName.setText("");
this.emloyeeDetailJmbg.setText("");
this.emloyeeDetailPosition.setText("");
this.employeeDetailContractType.setText("");
this.employeeDetailStatus.setText("");
}
}
答案 0 :(得分:1)
将项目添加到表格时,会立即选择第一个项目。这是在您使用选择模型的所选项属性注册侦听器之前发生的,因此不会为该初始选择调用侦听器的方法。
移动线
this.employeeModel.loadData();
employees = this.employeeModel.getEmployees();
employeeTableView.setItems(employees);
在使用所选项目注册侦听器之后,到initialize()
方法的末尾。