使用包含复选框的列生成tableview,并将侦听器添加到这些复选框

时间:2015-02-08 07:55:37

标签: javafx-8

如何在javafx 8中实现上述功能?我能够使用cellFactory构造一个带有CheckBoxTableCell的表列,但无法访问复选框以向其添加侦听器。那我该怎么办?

编辑----

import BillControl.Controller.PopulateView;
import BillControl.Controller.Validator;
import BillControl.MainApp;
import BillControl.model.Article;
import BillControl.model.Job;
import javafx.beans.property.*;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.control.cell.CheckBoxTableCell;
import javafx.stage.Stage;
import javafx.util.Callback;


import java.util.ArrayList;
import java.util.TreeSet;
import java.util.stream.Collectors;


public class ArticleJobAssignmentController {
    private Article article;
    private Stage jobAssignStage;
    private boolean okClicked = false;
    private MainApp mainApp;
    ArrayList<Job> selectedJobList = new ArrayList<>();
    private ObservableList<Job> masterJobList = FXCollections.observableArrayList();
    private ObservableList<Job> currentJobList = FXCollections.observableArrayList();
    private ObservableList<Job> articleEngagementList = FXCollections.observableArrayList();
    private TreeSet rowIndices = new TreeSet();
@FXML
private Label articleNameLabel;
@FXML
private Label noOfJobsLabel;
@FXML
private Button okButton;
@FXML
private Button cancelButton;
@FXML
private Label errorLabel;



@FXML
private TableView<Job> jobTableView;
@FXML
private TableColumn<Job, CheckBox> checkBoxTableColumn;
@FXML
private TableColumn<Job, String> jobNameColumn;
@FXML
private TableColumn<Job, String> clientNameColumn;
@FXML
private TableColumn<Job, Integer> noOfArticlesColumn;
@FXML
private TableColumn<Job, String> alreadyEngagedColumn;


public ArticleJobAssignmentController(){

}

public void initialize(){
    articleNameLabel.setText(article.getName());
    jobTableView.setItems(currentJobList);
    errorLabel.setVisible(false);
    jobTableView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
    jobTableView.setEditable(true);



    checkBoxTableColumn.setCellFactory(new Callback<TableColumn<Job, CheckBox>, TableCell<Job, CheckBox>>() {
        @Override
        public TableCell<Job, CheckBox> call(TableColumn<Job, CheckBox> param) {
            return new CheckBoxTableCell<Job, CheckBox>();
        }
    });


    jobNameColumn.setCellValueFactory(
            cellData -> cellData.getValue().nameProperty()
    );

    noOfArticlesColumn.setCellValueFactory(
            cellData -> {
                SimpleIntegerProperty sip = new SimpleIntegerProperty(cellData.getValue().numberOfArticlesProperty().getValue());
                ObservableValue<Integer> ov = sip.asObject();
                return ov;
            }

    );

    alreadyEngagedColumn.setCellValueFactory(
        cellData -> {
            Boolean engaged = false;
            for(Job job: articleEngagementList){
                if(job.getNumberID().equals(cellData.getValue().getNumberID())){
                    engaged = true;
                }
            }

            if(engaged){
                SimpleStringProperty sbp = new SimpleStringProperty("Yes");
                ObservableValue<String> ov = sbp;
                return ov;
            }
            else {
                SimpleStringProperty sbp = new SimpleStringProperty("No");
                ObservableValue<String> ov = sbp;
                return ov;
            }
        }
    );

    jobTableView.getSelectionModel().getSelectedItems().addListener(
            new ListChangeListener<Job>() {
                @Override
                public void onChanged(Change<? extends Job> c) {
                    noOfJobsLabel.setText(String.valueOf(c.getList().size()));
                }
            }
    );



    for(Job job : currentJobList){
        CheckBox cb = checkBoxTableColumn.getCellData(job);
        cb.selectedProperty().addListener(
                (obsv, oldv, newv) -> {
                    ArrayList<Job> tempSelectionArray = new ArrayList<>();

                    if(newv.booleanValue()){
                        tempSelectionArray.addAll(jobTableView.getSelectionModel().getSelectedItems().stream().collect(Collectors.toList()));
                        this.jobTableView.getSelectionModel().clearSelection();

                        for(Job arrayJob: tempSelectionArray){
                            jobTableView.getSelectionModel().select(arrayJob);
                        }

                        tempSelectionArray.clear();
                    }
                    else{
                        tempSelectionArray.addAll(this.jobTableView.getSelectionModel().getSelectedItems().stream().collect(Collectors.toList()));
                        this.jobTableView.getSelectionModel().clearSelection();
                        tempSelectionArray.remove(getJobTableView().getFocusModel().getFocusedItem());

                        for(Job arrayJob: tempSelectionArray){
                            this.jobTableView.getSelectionModel().select(arrayJob);
                        }

                        tempSelectionArray.clear();
                    }

                }
        );
    }


}

public void filterMasterList(){
    for(Job job : masterJobList){
        if(!job.getIsCompleted()){
            currentJobList.add(job);
        }
    }
}


@FXML
public void handleOkClicked(){
    if(!Validator.articleJobAssignment(this)){

        for(Job job : jobTableView.getSelectionModel().getSelectedItems()){
            selectedJobList.add(job);
        }
        okClicked = true;
        jobAssignStage.close();
    }
    else {
        errorLabel.setText("Select at least one job");
        errorLabel.setVisible(true);
    }
}

@FXML
public void handleCancelClicked(){
    jobAssignStage.close();
}

public void setArticle(Article article) {
    this.article = article;

}

public void setJobAssignStage(Stage jobAssignStage) {
    this.jobAssignStage = jobAssignStage;
}

public void setOkClicked(boolean okClicked) {
    this.okClicked = okClicked;
}

public void setMainApp(MainApp mainApp) {
    this.mainApp = mainApp;
    setMasterJobList(mainApp.getJobObservableList());
    filterMasterList();


    if(article != null){
        articleEngagementList = PopulateView.articleCurrentEngagementList(articleEngagementList, article.getId(), mainApp.getClientObservableList());
    }
}

public Label getArticleNameLabel() {
    return articleNameLabel;
}

public Label getNoOfJobsLabel() {
    return noOfJobsLabel;
}

public Button getOkButton() {
    return okButton;
}

public Button getCancelButton() {
    return cancelButton;
}

public TableView<Job> getJobTableView() {
    return jobTableView;
}

public TableColumn<Job, String> getJobNameColumn() {
    return jobNameColumn;
}

public TableColumn<Job, String> getClientNameColumn() {
    return clientNameColumn;
}

public TableColumn<Job, Integer> getNoOfArticlesColumn() {
    return noOfArticlesColumn;
}

public ObservableList<Job> getMasterJobList() {
    return masterJobList;
}

public void setMasterJobList(ObservableList<Job> masterJobList) {
    this.masterJobList = masterJobList;
}

public boolean isOkClicked() {
    return okClicked;
}

public ArrayList<Job> getSelectedJobList() {
    return selectedJobList;
}

}

1 个答案:

答案 0 :(得分:2)

JavaFX就是将模型视图分开。

您的CheckBoxTableCell查看ObservableValue<Boolean>模型

我们没有看到您的代码,但在某个地方您必须使用某种CheckBoxTableCell来初始化BooleanProperty。此属性由呈现的CheckBox中的TableView切换。

所以你只需要将侦听器注册到这个/这些属性/属性。


修改

您应该从

更改TableColumn
private TableColumn<Job, CheckBox> checkBoxTableColumn;

private TableColumn<Job, Boolean> checkBoxTableColumn;

此外,您的BooleanProperty课程需要Job。例如:

private final BooleanProperty test = new SimpleStringProperty(this, "test", false);

public BooleanProperty testProperty() {
    return test;
}

现在回到TableView

checkBoxTableColumn.setCellFactory(CheckBoxTableCell.forTableColumn(checkBoxTableColumn.setCellFactory());
checkBoxTableColumn.setCellValueFactory(c -> c.getValue().testProperty());