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; }
}
You are seeing the results of calling the
toString()
method on thePerson
object returned from the selection model. As you haven't overridden thetoString()
method, you are getting the result of the defaulttoString
method defined inObject
, which displays the class name andhashCode
.You can either add a
toString
method to thePerson
class:or just extract the data you need when you need it: