I have a tableView of id, name, mail, phone and select. I wanna get all selected row of my tableView and add selected mail to my email list in my model class. I read : this but I don't know how to add the listener to my obsevealList data to get what I want do that as a listener to data:
for (People p : model.getData()) {
if (p.getCheck())
model.getEmails().add(p.getMail());
}
}
How can I do this please ?
People class:
public class People {
private IntegerProperty id = new SimpleIntegerProperty(this, "id");
private StringProperty name = new SimpleStringProperty(this, "name");
private StringProperty mail = new SimpleStringProperty(this, "mail");
private StringProperty phone = new SimpleStringProperty(this, "phone");
private BooleanProperty check = new SimpleBooleanProperty(this, "check");
public People(String name, String mail, String phone) {
this.name.set(name);
this.mail.set(mail);
this.phone.set(phone);
this.check.set(false);
}
public People(Integer id,String name, String mail, String phone) {
this.id.set(id);
this.name.set(name);
this.mail.set(mail);
this.phone.set(phone);
this.check.set(false);
}
public IntegerProperty idProperty() {
return id;
}
public Integer getId() {
return idProperty().get();
}
public StringProperty nameProperty() {
return name;
}
public String getName() {
return nameProperty().get();
}
public void setName(String name) {
nameProperty().set(name);
}
public StringProperty mailProperty() {
return mail;
}
public String getMail() {
return mailProperty().get();
}
public void setMail(String mail) {
mailProperty().set(mail);
}
public StringProperty phoneProperty() {
return phone;
}
public String getphone() {
return phoneProperty().get();
}
public void setPhone(String phone) {
phoneProperty().set(phone);
}
public BooleanProperty checkProperty() {
return check;
}
public Boolean getCheck() {
return checkProperty().get();
}
public void setCheck(Boolean remark) {
checkProperty().set(remark);
}
}
My data class:
public class Data {
private Connection con = null;
private PreparedStatement ps = null;
private Statement st = null;
private ResultSet rs = null;
private List<String> emails;
private String mychoice;
private ObservableList<People> data = FXCollections.observableArrayList();
public ObservableList<People> getData(){
return data;
}
public List<String> getEmails() {
return emails;
}
public void addEmails(String mail) {
emails.add(mail);
}
public void setEmails(List<String> emails) {
this.emails = emails;
}
public String getMychoice() {
return mychoice;
}
public void setMychoice(String mychoice) {
this.mychoice = mychoice;
}
public void loadData() {
try {
con=getConnect();
data.clear();
String query = "SELECT * FROM " + mychoice;
ps = con.prepareStatement(query);
rs = ps.executeQuery();
while (rs.next()) {
data.add(new People(
rs.getInt(1),
rs.getString(2),
rs.getString(3),
rs.getString(4)
));
}
rs.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
extrait of Show class:
public class Show implements Initializable {
Data model=new Data();
@FXML
private TableView<People> table;
@FXML
private TableColumn<People, String> name,mail, phone;
@FXML
private TableColumn<People, Integer> id;
@FXML
private TableColumn <People,Boolean> select;
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
con = getConnect();
loadTable(); //set all table name inside the combobox
table.setItems(model.getData());
id.setCellValueFactory(cd -> cd.getValue().idProperty().asObject());
name.setCellValueFactory(cd -> cd.getValue().nameProperty());
phone.setCellValueFactory(cd -> cd.getValue().phoneProperty());
mail.setCellValueFactory(cd -> cd.getValue().mailProperty());
table.setEditable(true);
name.setCellFactory(TextFieldTableCell.forTableColumn());
mail.setCellFactory(TextFieldTableCell.forTableColumn());
phone.setCellFactory(TextFieldTableCell.forTableColumn());
select.setCellFactory(CheckBoxTableCell.forTableColumn(select));
select.setCellValueFactory(cd -> cd.getValue().checkProperty());
select.setCellFactory(CheckBoxTableCell.forTableColumn(new Callback<Integer, ObservableValue<Boolean>>() {
@Override
public ObservableValue<Boolean> call(Integer param) {
System.out.println("Contact " + model.getData().get(param).getMail() + " changed value to " + model.getData().get(param).getCheck());
return model.getData().get(param).checkProperty();
}
}));
}
}