JavaFX: CheckBoxTableCell get ActionEvent when user check a checkBox

前端 未结 1 868
感情败类
感情败类 2020-12-01 17:36

I want to trigger a method or action when the user check or uncheck a checkbox in the tableView. the coursData.addListener(...) doesn\'t get triggered when the user use the

1条回答
  •  有刺的猬
    2020-12-01 18:10

    You have two ways of getting a notification when any of the check boxes is clicked.

    One: providing a callback as argument for CheckBoxTableCell.forTableColumn instead of the tableColumn:

    checkedCol.setCellFactory(CheckBoxTableCell.forTableColumn(new Callback>() {
    
        @Override
        public ObservableValue call(Integer param) {
            System.out.println("Cours "+items.get(param).getCours()+" changed value to " +items.get(param).isChecked());
            return items.get(param).checkedProperty();
        }
    }));
    

    Two: providing a callback to the collection:

    final List items=Arrays.asList(new Cours("Analyse", "3"), 
              new Cours("Analyse TP", "4"), 
              new Cours("Thermo", "5"),
              new Cours("Thermo TP", "7"),
              new Cours("Chimie", "8"));
    this.coursData = FXCollections.observableArrayList(new Callback() {
    
        @Override
        public Observable[] call(Cours param) {
            return new Observable[] {param.checkedProperty()};
        }
    });
    coursData.addAll(items);
    

    and now listening to changes in the collection:

    coursData.addListener(new ListChangeListener() {
    
        @Override
        public void onChanged(ListChangeListener.Change c) {
            while (c.next()) {
                if (c.wasUpdated()) {
                    System.out.println("Cours "+items.get(c.getFrom()).getCours()+" changed value to " +items.get(c.getFrom()).isChecked());
                }
              }
        }
    });
    

    0 讨论(0)
提交回复
热议问题