【问题标题】:JavaFX: CheckBoxTableCell get ActionEvent when user check a checkBoxJavaFX:当用户选中复选框时,CheckBoxTableCell 获取 ActionEvent
【发布时间】:2015-04-24 14:53:01
【问题描述】:

我想在用户选中或取消选中 tableView 中的复选框时触发方法或操作。当用户使用 checkBox 时,不会触发 courseData.addListener(...) 。

这是我编译的代码,窗口显示为带有复选框的 tableView。

package testCheckBox2;

import javafx.application.Application;
import javafx.beans.InvalidationListener;
import javafx.beans.Observable;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonBase;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.CheckBoxTableCell;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;


public class CheckBoxTableCellTest extends Application {

  private TableView<cours> tableView;

  public ObservableList<cours> coursData ;//= FXCollections.observableArrayList();
  @Override

  public void start(Stage primaryStage) {

      this.tableView = new TableView<cours>();
      final TableColumn<cours, String> Cours = new TableColumn<cours, String>("Cours");
      final TableColumn<cours, Boolean> checkedCol = new TableColumn<cours, Boolean>("Checked");

      this.coursData  =FXCollections.observableArrayList(
                new cours("Analyse", "3"), 
                new cours("Analyse TP", "4"), 
                new cours("Thermo", "5"),
                new cours("Thermo TP", "7"),
                new cours("Chimie", "8"));

    tableView.setItems(this.coursData);

    tableView.getColumns().addAll(Cours, checkedCol);

    Cours.setCellValueFactory(new PropertyValueFactory<cours, String>("cours"));

    checkedCol.setCellValueFactory(new PropertyValueFactory<cours, Boolean>("checked"));

    checkedCol.setCellFactory(CheckBoxTableCell.forTableColumn(checkedCol));
    checkedCol.setEditable(true);
    tableView.setEditable(true);

    final BorderPane root = new BorderPane();
    root.setCenter(tableView);

    coursData.addListener(new InvalidationListener() {

        @Override public void invalidated(Observable o) {
                System.out.println("checkBox change state ");
                //Here is my problem. 
                //When the user click on a checkBox , the method isn't call .
            }
        });
Scene scene = new Scene(root, 300, 400);
    primaryStage.setScene(scene);
    primaryStage.show();
  }

  public static void main(String[] args) {
    launch(args);
  }

  public static class cours {
    private StringProperty cours;
    private StringProperty coursID;
    private BooleanProperty checked;

    public cours(String cours, String coursID) {
      this.cours = new SimpleStringProperty(cours);
      this.coursID = new SimpleStringProperty(coursID);
      this.checked = new SimpleBooleanProperty(false);
    }

    public String getCours() {
      return cours.get();
    }

    public String getCoursID() {
      return coursID.get();
    }

    public boolean isChecked() {
      return checked.get();
    }

    public void setCours(String cours) {
      this.cours.set(cours);
    }

    public void setCoursID(String coursID) {
      this.coursID.set(coursID);
    }

    public void setChecked(boolean checked) {
      this.checked.set(checked);
    }

    public StringProperty coursProperty() {
      return cours;
    }

    public StringProperty coursIDProperty() {
      return coursID;
    }

    public BooleanProperty checkedProperty() {
      return checked;
    }
  }
}

【问题讨论】:

    标签: java checkbox javafx tableview


    【解决方案1】:

    当任何一个复选框被点击时,您有两种方式获得通知。

    一:提供回调作为CheckBoxTableCell.forTableColumn 的参数而不是tableColumn:

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

    二:为集合提供回调:

    final List<Cours> 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<Cours, Observable[]>() {
    
        @Override
        public Observable[] call(Cours param) {
            return new Observable[] {param.checkedProperty()};
        }
    });
    coursData.addAll(items);
    

    现在正在监听集合中的变化:

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

    【讨论】:

    • 完美解决方案!谢谢(我选择了第二种方式)。非常感谢 !!!! :)
    • 刚刚尝试过。无论我导入哪个回调,它都不起作用。您导入了哪个回调?
    • 请创建一个新问题
    • 我如何为param.checkedProperty1()param.checkedProperty2()param.checkedProperty3()等多个检查属性编写此return new Observable[] {param.checkedProperty()};
    • @JoséPereda 我实现了第一个解决方案并发现了一个问题。正如您所期望的那样,“侦听器”将在点击时执行,但也会在将行添加到 tableview 或滚动 tableview 时执行。
    猜你喜欢
    • 2018-04-06
    • 2021-11-08
    • 2020-07-02
    • 2018-06-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-13
    • 2012-09-13
    • 1970-01-01
    相关资源
    最近更新 更多