jeudi 24 août 2017

JavaFx : get cell graphic from tableView

Maybe my question is duplicate ,but i searched before i ask ,and i don't found any answer. I have a tableView and it has a rows with checkbox. What I want to do is on a checkBox selected , I want to select all checkBoxes in tableView. So far I managed to create checkboxes in a tableView. The code is as follow.

@FXML
javafx.scene.control.TableView<Bill> BillView;
@FXML
javafx.scene.control.TableColumn<Bill, String> BillNameCo;
@FXML
javafx.scene.control.TableColumn<Bill, String> UnitBillPriceCo;
@FXML
javafx.scene.control.TableColumn<Bill, String> WeightBillCo;
@FXML
javafx.scene.control.TableColumn<Bill, String> BillPriceCo;
@FXML
javafx.scene.control.TableColumn<Bill, String> BillTVACo;
@FXML
javafx.scene.control.TableColumn<Bill, String> BillTotalHTCo;
@FXML
javafx.scene.control.TableColumn<Bill, String> BillTotalTVACo;
@FXML
javafx.scene.control.TableColumn<Bill, String> UnitBillCo;
@FXML
javafx.scene.control.TableColumn<Bill, String> BillTotalCo;
@FXML
javafx.scene.control.TableColumn<Bill, Boolean> rowSelectedCoBill;
@FXML
 javafx.scene.control.CheckBox allRows;

This is the part that defines the cellFactories

public class CheckBoxCellFactory implements Callback {

    @Override
    public TableCell call(Object param) {
        CheckBoxTableCell<Bill, Boolean> checkBoxCell = new CheckBoxTableCell();
        return checkBoxCell;
    }
}

public void setCellValueFactoryBillView() {
    BillNameCo.setCellValueFactory(new PropertyValueFactory<>("desgination"));
    WeightBillCo.setCellValueFactory(new PropertyValueFactory<>("amount"));
    UnitBillCo.setCellValueFactory(new PropertyValueFactory<>("unit"));
    BillPriceCo.setCellValueFactory(new PropertyValueFactory<>("unitPrice"));
    BillTVACo.setCellValueFactory(new PropertyValueFactory<>("percentVAT"));
    BillTotalHTCo.setCellValueFactory(new PropertyValueFactory<>("priceHT"));
    BillTotalTVACo.setCellValueFactory(new PropertyValueFactory<>("priceVAT"));
    UnitBillPriceCo.setCellValueFactory(new PropertyValueFactory<>("unitOfPrice"));
    BillTotalCo.setCellValueFactory(new PropertyValueFactory<>("total"));
    rowSelectedCoBill.setCellValueFactory(new PropertyValueFactory<>("checked"));
    rowSelectedCoBill.setCellFactory(new Callback<TableColumn<Bill, Boolean>, TableCell<Bill, Boolean>>() {

        public TableCell<Bill, Boolean> call(TableColumn<Bill, Boolean> p) {
            return new CheckBoxTableCell<Bill, Boolean>();
        }
    });

}

public static class CheckBoxTableCell<S, T> extends TableCell<S, T> {

    private final CheckBox checkBox;
    private ObservableValue<T> ov;

    public CheckBoxTableCell() {
        this.checkBox = new CheckBox();
        this.checkBox.setAlignment(Pos.CENTER);

        setAlignment(Pos.CENTER);
        setGraphic(checkBox);
    }

    @Override
    public void updateItem(T item, boolean empty) {
        super.updateItem(item, empty);
        if (empty) {
            setText(null);
            setGraphic(null);
        } else {
            setGraphic(checkBox);
            if (ov instanceof BooleanProperty) {
                checkBox.selectedProperty().unbindBidirectional((BooleanProperty) ov);
            }
            ov = getTableColumn().getCellObservableValue(getIndex());
            if (ov instanceof BooleanProperty) {
                checkBox.selectedProperty().bindBidirectional((BooleanProperty) ov);

            }
        }
    }
}

Until now all is well and works correctly.select all checkBoxes in cells

Now ,I want when checkBox (idFx AllRows) is selected all checkBoxes in cell are selected too,so how i can implement it ? or How i can get graphic from cells ?

       public void selectAllRow() {
                    //how i can select all checkBoxes from this method?
                }




Aucun commentaire:

Enregistrer un commentaire