mercredi 21 avril 2021

Javafx TableView with Checkbox

I have the following classes and i need to do a listener on Checkbox in TableView. Also have a ProgressBar, which i need to work based on checked Checkboxes. I did it with ListView(in code below too), and i need something similar with my TableView + color rows, in which are checked Checkboxes

Controller:

public class Controller {
@FXML
private TextField textField2;
@FXML
private TableColumn<Task,String> columnnTime;
@FXML
private TableColumn<Task,String> columnnWork;
@FXML
private TableColumn<Task,CheckBox> columnDone;
@FXML
private TableView<Task> tableViewTabulka;
@FXML
private Label labelDay;
@FXML
private TextField textField;
@FXML
private ListView listView = new ListView<String>();
@FXML
private ProgressBar progressBar;
private Task task;
ObservableSet<Task> selected = FXCollections.observableSet();

 @FXML
private void initialize() {
    addLabels();
    fillInLabelP();
    columnnTime.setCellValueFactory(new PropertyValueFactory<>("time"));
    columnnWork.setCellValueFactory(new PropertyValueFactory<>("work"));
    columnDone.setCellValueFactory(new PropertyValueFactory<>("done"));
}

Task:

public class Task {
private String time;
private String work;
private CheckBox done;


public Task(String time, String work, CheckBox done) {
    this.time = time;
    this.work = work;
    this.done = done;
}

public String getTime() {
    return time;
}

public void setTime(String time) {
    this.time = time;
}

public String getWork() {
    return work;
}

public void setWork(String work) {
    this.work = work;
}

public CheckBox getDone() {
    return done;
}

public void setDone(CheckBox done) {
    this.done = done;
}

My working ListView from a previous experiment:

listView.setCellFactory(CheckBoxListCell.forListView(new Callback<Item, ObservableValue<Boolean>>() {
        @Override
        public ObservableValue<Boolean> call(Item item) {

            item.onProperty().addListener((obs, wasSelected, isNowSelected) -> {
                if (isNowSelected) {
                    selected.add(item);
                } else {
                    selected.remove(item);
                }
                double y = listView.getItems().size();
                double x= selected.size();
                double kon = ((1/y)*x);

                progressBar.setProgress(kon);
            });
            return item.onProperty();
        }
    }));

}

TableView: enter image description here




Aucun commentaire:

Enregistrer un commentaire