生成带有包含复选框的列的表视图,并将列表器添加到这些复选框 [英] Generate a tableview with a column containing a checkboxes and add listners to those checkboxes

查看:86
本文介绍了生成带有包含复选框的列的表视图,并将列表器添加到这些复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How do i achieve above functionality in javafx 8? I was able to construct a table column with a CheckBoxTableCell using the cellFactory but was not able to access the checkboxes to add a listener to them. So how should i proceed?

编辑----

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;
}

}

推荐答案

JavaFX的全部目的在于分离 Model View .

JavaFX is all about separating Model and View.

您的CheckBoxTableCell视图,而ObservableValue<Boolean>模型.

我们看不到您的代码,但是您必须在某处用某种BooleanProperty初始化CheckBoxTableCell.此属性由呈现的TableView中的CheckBox切换.

We do not see your code, but somewhere you have to initialize your CheckBoxTableCellwith a BooleanProperty of some sorts. This property gets toggled by the CheckBox in the rendered TableView.

因此,您只需要向此/这些属性注册侦听器.

So you just have to register listener(s) to this / these property / properties.

您应将TableColumn从更改为

private TableColumn<Job, CheckBox> checkBoxTableColumn;

private TableColumn<Job, Boolean> checkBoxTableColumn;

此外,您在Job类中将需要一个BooleanProperty.例如:

Further you will need a BooleanProperty in your Job class. For example:

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());

这篇关于生成带有包含复选框的列的表视图,并将列表器添加到这些复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆