A
A
Andrej Kopp2019-05-23 17:39:16
Java
Andrej Kopp, 2019-05-23 17:39:16

How to get ID on click on checkbox in JavaFX?

Hello. I'm having trouble getting event data. I need that when clicking on the checkbox, which are displayed through the CheckBoxTableCell in the TableView column, I could get true or false. Now only one checkbox works for me, which selects all the others and displays the record IDs from my model in the console. Here is what I have:
Controller

@FXML
    private void initialize() {
selectedColumn.setCellFactory(CheckBoxTableCell.forTableColumn(selectedColumn));
        select_all.selectedProperty().addListener((observable, oldValue, newValue) -> {
            for(Person person : personTable.getItems()) {
                person.setSelected(newValue);
                if(newValue == true) {
                    int count = personTable.getItems().size();
                    getDeleteAllPersBtn().setText("Delete (" + count + ")");
                    personTable.getSelectionModel().select(person.getPersonId());
                    System.out.println(person.getPersonId());
                } else {
                    personTable.getSelectionModel().clearSelection(person.getPersonId());
                    getDeleteAllPersBtn().setText("Delete");
                }
            }

            getDeleteAllPersBtn().setDisable(!select_all.isSelected());
        });
}

Model
public class Person {
    private SimpleBooleanProperty selected;
    private IntegerProperty id;

    //Constructor
    public Person() {
        this.selected = new SimpleBooleanProperty(false);
        this.id = new SimpleIntegerProperty();
    }

//selected
    public BooleanProperty selectedProperty() {
        return selected;
    }

    public boolean getSelected() {
        return selected.get();
    }

    public void setSelected(boolean selected) {
        this.selected.set(selected);
    }

}

FXML
<TableColumn fx:id="selectedColumn" prefWidth="50.0" style="-fx-alignment: CENTER;">
                           <graphic>
                              <CheckBox fx:id="select_all" mnemonicParsing="false" />
                           </graphic>
                           <cellValueFactory>
                              <PropertyValueFactory property="selected" />
                           </cellValueFactory>
                        </TableColumn>

Here are the sources.

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