Hello everyone. Faced such a problem. It is necessary when clicking the checkbox, which is on top, toggle the checkboxes in the box next to the entire list in the table. And then when you click on the button, delete the selected records, delete the selected records from the MySQL database.
Here is what was done.
PersonUnpersonValueFactoryController.java
package usersapp.controller;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.CheckBox;
import javafx.scene.control.TableColumn;
import javafx.util.Callback;
import usersapp.model.Person;
public class PersonUnpersonValueFactory implements Callback<TableColumn.CellDataFeatures<Person, CheckBox>, ObservableValue<CheckBox>> {
@Override
public ObservableValue<CheckBox> call(TableColumn.CellDataFeatures<Person, CheckBox> param) {
Person person = param.getValue();
CheckBox checkBox = new CheckBox();
checkBox.selectedProperty().setValue(person.isUnperson());
checkBox.selectedProperty().addListener((ov, old_val, new_val) -> {
person.setUnperson(new_val);
System.out.println(new_val);
});
return new SimpleObjectProperty<>(checkBox);
}
}
Person.java
public class Person {
private Boolean unperson;
...
//unperson
public Boolean isUnperson() {
return this.unperson;
}
public void setUnperson(Boolean unperson){
this.unperson = unperson;
}
...
}
PersonView.fxml
<?import usersapp.controller.PersonUnpersonValueFactory?>
...
<TableView fx:id="personTable" editable="true" layoutX="7.0" layoutY="53.0" prefHeight="285.0" prefWidth="378.0" tableMenuButtonVisible="true" AnchorPane.bottomAnchor="4.0" AnchorPane.leftAnchor="7.0" AnchorPane.rightAnchor="7.0" AnchorPane.topAnchor="53.0">
<columns>
<TableColumn prefWidth="50.0" style="-fx-alignment: CENTER;">
<cellValueFactory>
<PersonUnpersonValueFactory />
</cellValueFactory>
<graphic>
<CheckBox mnemonicParsing="false" />
</graphic>
</TableColumn>
...
</columns>
</TableView>
I would be very grateful for the help.
You would just need a listener on the "master" CheckBox
to watch the selectedProperty
for changed. Then you could use that to iterate through the TableView
items and update their selectedProperty
as well.
Note that with the limited code you've provided, I need to make some assumptions on how your data model is constructed.
checkBox.selectedProperty().addListener((observable, oldValue, newValue) -> {
// Loop through entire TableView to set the selected property for each Item
for (Item item : tableView.getItems()) {
item.setSelected(newValue);
}
});