Checkbox selected bind with TableView


Question

How to bind with TableView selection model enter image description herewith checkbox isSelected in table view.In manually (using mouse multiple select) select the item in tableview

 ObservableList<T> observableList = tableView.getSelectionModel().getSelectedItems();
 System.out.println(observableList);

how to bind checkbox with tableview

   box.selectedProperty().addListener(new ChangeListener<Boolean>() {
                            @Override
                            public void changed(ObservableValue<? extends Boolean> ov, Boolean t, Boolean t1) {
                                if (t1) {
                                    Object object = getTableView().getItems().get(getTableRow().getIndex());
                                    objects.add(object);
                                    getTableView().getItems().set(getTableRow().getIndex(), object);
                                    tableView.getSelectionModel().getSelectedItem();                                    //tableView.getSelectionModel().setSelectedIndex(getTableRow().getIndex());

                                } else {
                                    Object object = getTableView().getItems().get(getTableRow().getIndex());
                                    objects.remove(object);
                                }
                                System.out.println(objects);
                            }
                        });

if Checkbox is selected in row that row item bind with getSelectionModel().getSelectedItems() or how set manually setSelectedItems in tableView

1
1
1/28/2014 9:51:13 AM

Accepted Answer

Add to this line in box.selectedProperty().addListener()

tableView.getSelectionModel().select(getTableRow().getIndex());

box.selectedProperty().addListener(new ChangeListener<Boolean>() {
    @Override
    public void changed(ObservableValue<? extends Boolean> ov, Boolean t, Boolean t1) {
        if (t1) {
            tableView.getSelectionModel().select(getTableRow().getIndex());

        } else {
            tableView.getSelectionModel().clearSelection(getTableRow().getIndex());
        }
    }
});
1
10/15/2015 8:43:29 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon