A
A
Andrej Kopp2019-05-07 16:21:11
Java
Andrej Kopp, 2019-05-07 16:21:11

How to make checkbox with selection of all checkboxes in TableView in JavaFX and FXML?

5cd182cc666ea722736895.png
Hello. Faced such a problem. It is necessary, when clicking the checkbox, which is on top, to switch the checkboxes in the field of 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's what we managed to pick up:
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 help.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question