Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

My TableCell doesn't auto-update FXCollections.observableArrayList

I’m sorry I get some trouble with TableView.

If I tick checkbox my dati = FXCollections.observableArrayList(); isn’t auto-updated
Where I am wrong? I have missing some code.
I set editable the TableView. Table has two rows and each row have checkbox.

In agreement FXCollections.observableArrayList(); documentation

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Creates a new empty observable list that is backed by an arraylist.

Therefore it Should be auto updated by TableView.

package unimi.sysvotes.anagrafica;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.geometry.Pos;
import javafx.scene.control.CheckBox;
import javafx.scene.control.TableCell;

public class BooleanCell extends TableCell<AmministratoreRiga, Boolean> {
    private CheckBox checkBox;
    public BooleanCell() {
        checkBox = new CheckBox();
        checkBox.setDisable(true);
        checkBox.selectedProperty().addListener(new ChangeListener<Boolean> () {
            public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
                if(isEditing()){
                    System.out.println(newValue);
                    commitEdit(newValue == null ? false : newValue);
                
                }
            }
        });
        this.checkBox.setAlignment(Pos.CENTER);

        setAlignment(Pos.CENTER);
        setGraphic(checkBox);

    }
    @Override
    public void startEdit() {
        super.startEdit();
        if (isEmpty()) {
            return;
        }
        checkBox.setDisable(false);
        checkBox.requestFocus();
    }
    @Override
    public void cancelEdit() {
        super.cancelEdit();
        checkBox.setDisable(true);
    }
    public void commitEdit(Boolean value) {
        super.commitEdit(value);
        checkBox.setDisable(true);
    }
    @Override
    public void updateItem(Boolean item, boolean empty) {
        super.updateItem(item, empty);
        if (!isEmpty()) {
            checkBox.setSelected(item);
        }else{
            setGraphic(null);
        }
    }
}

EDIT:

public class NuovaElezioneController {
   @FXML
   private TableView<AmministratoreRiga> amministratoreTabella;

   private ObservableList<AmministratoreRiga> dati;
   
   @FXML
   private TableColumn<AmministratoreRiga,Boolean> selezioneColonna;
   
   @FXML
   private TableColumn<AmministratoreRiga,Integer>  idUtenteColonna;

   @FXML
   private TableColumn<AmministratoreRiga,String>  nomeColonna;
   
   @FXML
   private TableColumn<AmministratoreRiga,String> cognomeColonna;

   @FXML
   private TableColumn<AmministratoreRiga,String> ruoloColonna;

   //@FXML
   //private TableColumn<AmministratoreRiga,Void> abilitaColonna;


   @FXML
   private void initialize() {
       dati = FXCollections.observableArrayList();

       // Add a change listener to the list 
       dati.addListener(new ListChangeListener<AmministratoreRiga>() { 
           @Override 
           public void onChanged(ListChangeListener.Change<? extends AmministratoreRiga> change) { 
                   System.out.println("List has changed."); 
           } 
       });

       dati.addAll(new AmministratoreRiga(true,1,"Nicola","Paganotti","Utente"),
       new AmministratoreRiga(false,2,"Riccardo","Paganotti","Amministratore"));
       
       amministratoreTabella.setItems(dati);

       Callback<TableColumn<AmministratoreRiga, Boolean>, TableCell<AmministratoreRiga, Boolean>> booleanCellFactory = 
           new Callback<TableColumn<AmministratoreRiga, Boolean>, TableCell<AmministratoreRiga, Boolean>>() {
           @Override
               public TableCell<AmministratoreRiga, Boolean> call(TableColumn<AmministratoreRiga, Boolean> p) {
                   return new BooleanCell();
           }
       };

       selezioneColonna.setCellValueFactory(new PropertyValueFactory<AmministratoreRiga,Boolean>("selezione"));
       selezioneColonna.setCellFactory(booleanCellFactory); 

>Solution :

It might be easier to answer if you would also post the main class. I suppose you set the CheckBox of the TableCell after you added that Table Cell to your TableView. You should first set the CheckBox to your TableCell, and then the TableCell to the TableView. Then it should work as expected.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading