Sysrq147
Sysrq147

Reputation: 1367

JavaFX and Table View - First time selection won't work

I am developing simple JavaFX application. In my main view I have Table View. On row select method selectedEmployeeDetails() is called. It fills Labels which show details of employee (name, lastname..etc). When I start my app and select first row (only first row) nothing is happening. When I select secound row everything is fine. Then when I select first row again everyting works normal. Here is my code inside initialize method of main view's controller.

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

     });

}

Can someone help me to see what is the main reason my first row wont fire selectedEmployeeDetails() method on first time select.

EDIT

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("");

        }
    }

Upvotes: 0

Views: 391

Answers (1)

James_D
James_D

Reputation: 209694

When you add the items to the table, the first item becomes selected immediately. This happens before you register the listener with the selection model's selected item property, so the listener's method isn't invoked for that initial selection.

Move the lines

  this.employeeModel.loadData();
  employees = this.employeeModel.getEmployees();
  employeeTableView.setItems(employees); 

to the end of the initialize() method, after you register the listener with the selected item.

Upvotes: 1

Related Questions