Detect change in a CheckBox within a TableView

0

I have an observable list whose elements are shown in a table, one of whose columns is a CheckBox that can be modified by the user by clicking on them with the mouse. I want that when these CheckBox change I am notified to store those changes in a database.

The CheckBox column is this:

TableColumn<Person, Boolean> acceptedCol = new TableColumn<>("Accepted");
acceptedCol.setCellValueFactory(new PropertyValueFactory<>("accepted"));
acceptedCol.setCellFactory((TableColumn<Person, Boolean> p) -> {
    CheckBoxTableCell<Person, Boolean> cell = new CheckBoxTableCell<>();
    cell.getStyleClass().add("okGrasa");
    cell.setAlignment(Pos.CENTER);            
    return cell;
    });

The list consists of objects of class Person :

public class Person {
    private String name;
    private BooleanProperty accepted;

    public Person(String name, boolean accepted) {
        this.name = name;    
        this.accepted = new SimpleBooleanProperty(accepted);
    }  
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }   
    public BooleanProperty acceptedProperty() {
        return accepted;
    }
    public boolean isAccepted() {
        return acceptedProperty().get();
    }
}

I have filled the table with the list:

ObservableList<Person> list = getPersonList();
TableView<Person> table = new TableView<>();
table.setItems(list); 

And I've tried this:

list.addListener(new ListChangeListener() { 
    @Override
    public void onChanged(ListChangeListener.Change c) { 
        System.out.println ("Ha habido un cambio");
    }
});

But it only detects changes in the observable list but does not detect the editing of each one of the elements that make it up. Why?

    
asked by Zuri 12.01.2018 в 15:51
source

2 answers

1

The 'Person' class is correct, not the CellFactory you use.

Here you have a functional program with the appropriate corrections. It is necessary to implement the style sheet in the file "HojaDeStililo.css" that you have to place in the same folder as this code with a tag ".laClaseEstiloPersonal". Make sure you have version 9 of Java JDK installed since in previous versions this code gives problems. (Based on responses from the English version of StackOverflow: link link

public class CheckBoxTableCellTest extends Application {

  TableView<Person> tabla  = new TableView<>();     
  ObservableList<Person> lista ;

  @Override
  public void start(Stage primaryStage) {

    TableColumn<Person, Boolean> acceptedCol = new TableColumn<>("Aceptado");
    List<Person> items = Arrays.asList(new Person());
    lista = FXCollections.observableArrayList(new Person());

    tabla.setItems(this.lista);
    tabla.getColumns().addAll(acceptedCol);

    acceptedCol o.setEditable(true);
    tabla.setEditable(true);

    acceptedCol.setCellFactory((TableColumn<Person, Boolean> p) -> {
        final CheckBoxTableCell<Person, Boolean> cell = new CheckBoxTableCell<>();
        final BooleanProperty selected = new SimpleBooleanProperty();
        cell.setSelectedStateCallback((Integer index) -> lista.get(index).propiedadAceptado());        
        cell.getStyleClass().add("okGrasa");           
        return cell;
    });               

    lista = FXCollections.observableArrayList((Person param) -> new Observable[]{param.propiedadAceptado()});    
    lista.addListener((Observable o) -> {
        System.out.println("Cambia el checkBox");    
    });

    lista.addAll(items); 
    BorderPane root = new BorderPane();
    root.setCenter(tabla);
    Scene scene = new Scene(root, 300, 400);
    primaryStage.setScene(scene);
    primaryStage.show();    
  }
}

class Person {
    private BooleanProperty aceptado;

    public Person() {
      this.aceptado = new SimpleBooleanProperty(false);
    }
    public boolean isAceptado() {
      return aceptado.get();
    }
    public void setAceptado(boolean checked) {
      this.aceptado.set(checked);
    }
    public BooleanProperty propiedadAceptado() {
      return aceptado;
    }
}
    
answered by 19.01.2018 / 15:46
source
0

You can try this:

 tuCheckBox.selectedProperty().addListener(new ChangeListener<Boolean>() {
     public void changed(ObservableValue ov,Boolean old_val,Booleannew_val) {
        System.out.println(new_val);
     }
   });
    
answered by 12.01.2018 в 16:29