JavaFX:CheckBoxTableCell当用户检查复选框时获取ActionEvent [英] JavaFX: CheckBoxTableCell get ActionEvent when user check a checkBox

查看:1329
本文介绍了JavaFX:CheckBoxTableCell当用户检查复选框时获取ActionEvent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在用户选中或取消选中tableView中的复选框时触发方法或操作。当用户使用checkBox时,coursData.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应用程序{

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(Analyze,3),
new cours(Analyze 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 );
//这是我的问题。
//当用户点击一个复选框,该方法不会调用
}
});
场景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;
}
}
}


解决方案>

一个:为提供一个回调作为参数



  checkCol.setCellFactory(CheckBoxTableCell.forTableColumn(new Callback< Integer,ObservableValue< Boolean>>(){

@Override
public ObservableValue< Boolean> call(Integer param){
System.out.println(Cours+ items.get(param).getCours()+将值更改为+ items.get(param).isChecked());
return items.get(param).checkedProperty();
}
}));

二:提供对集合的回调:

  final List< Cours> items = Arrays.asList(new Cours(Analyze,3),
new Cours(Analyze 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& c){
while(c.next()){
if(c.wasUpdated()){
System.out.println(Cours+ items.get(c.getFrom ())。getCours()+changed value to+ items.get(c.getFrom())。isChecked());
}
}
}
} );


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 checkBox .

Here is my code it compiles and the windows appears with the tableView with checkbox.

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

解决方案

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

Two: providing a callback to the collection:

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

and now listening to changes in the collection:

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

这篇关于JavaFX:CheckBoxTableCell当用户检查复选框时获取ActionEvent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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