How see content column selecting with cursor

44 Views Asked by At

I need to know how I can view the contents of a cell depending on which is selected. I have to return the value of idPerson with method btnClick.

Thus goes the memory address but I want to return the id value I have selected

Thanks.

public class TablaFXML {
@FXML
TableView tablePeople;
@FXML
TableColumn idPersonColumn;
@FXML
TableColumn nameColumn;
@FXML
TableColumn surnameColumn;
@FXML
TableColumn emailColumn;
@FXML
TextArea textArea;
@FXML
Person pers;

@FXML
void initialize() {
    ObservableList<Person> data = FXCollections.observableArrayList(

    new Person("1", "Jacob", "Smith", "[email protected]"),

    new Person("2", "Isabella", "Johnson", "[email protected]"),

    new Person("3", "Ethan", "Williams", "[email protected]"),

    new Person("4", "Emma", "Jones", "[email protected]"),

    new Person("5", "Michael", "Brown", "[email protected]")

    );

    idPersonColumn
            .setCellValueFactory(new PropertyValueFactory("idPerson"));
    nameColumn.setCellValueFactory(new PropertyValueFactory("name"));
    surnameColumn.setCellValueFactory(new PropertyValueFactory("surname"));
    emailColumn.setCellValueFactory(new PropertyValueFactory("email"));

    tablePeople.setItems(data);

}

@FXML public void btnIdClick(){

    //int guardar = tablePeople.getSelectionModel().getFocusedIndex();


    System.out.println(tablePeople.getSelectionModel().getSelectedItem());

}

}

public class Person {

private StringProperty idPerson;
private StringProperty name;
private StringProperty surname;
private StringProperty email;



public Person(String id,String fName, String lName, String email) {

    this.idPerson = new SimpleStringProperty(id);
    this.name = new SimpleStringProperty(fName);
    this.surname = new SimpleStringProperty(lName);
    this.email = new SimpleStringProperty(email);

}



public StringProperty idPersonProperty() { return idPerson; }
public StringProperty nameProperty() { return name; }
public StringProperty surnameProperty() { return surname; }
public StringProperty emailProperty() { return email; }

}

1

There are 1 best solutions below

0
On BEST ANSWER

You are seeing the results of calling the toString() method on the Person object returned from the selection model. As you haven't overridden the toString() method, you are getting the result of the default toString method defined in Object, which displays the class name and hashCode.

You can either add a toString method to the Person class:

public class Person {

    // ... all code as before

    @Override
    public String toString() {
        return name.get() + " " + surname.get(); // any implementation you need
    }
}

or just extract the data you need when you need it:

@FXML public void btnIdClick() {
    Person selectedPerson = tablePeople.getSelectionModel().getSelectedItem() ;
    System.out.println(selectedPerson.firstNameProperty().get() + " " +
        selectedPerson.lastNameProperty().get());
}