简体   繁体   中英

JavaFX 2 - Tableview Checkbox not showing value

I finally managed to track whenever a click happens on a checkbox in my GUI. But now it won't load the correct value of the CheckBox into the gui. Can anyone see what might be wrong? :)

roadtrainCol.setCellValueFactory(
            new Callback<CellDataFeatures<Spot,Boolean>,ObservableValue<Boolean>>()
            {
                public ObservableValue<Boolean> call(CellDataFeatures<Spot, Boolean> param) {
                    return param.getValue().getRoadtrain();
                }   
            });
    roadtrainCol.setCellFactory(col -> {
        CheckBoxTableCell<Spot, Boolean> cell = new CheckBoxTableCell<>(index -> {
            BooleanProperty active = new SimpleBooleanProperty();
            active.addListener((obs, wasActive, isNowActive) -> {
                System.out.println("del");
                rettedeSpots(tableViewId.getItems().get(index).getId());
            });
            return active ;
        });
        return cell ;
    });

You're creating new BooleanProperty s. Those properties will always be initialized with false .

The cellFactory is not the place to add listeners. Add listeners to the items instead.

roadtrainCol.setCellValueFactory(
        new Callback<CellDataFeatures<Spot, Boolean>, ObservableValue<Boolean>>() {
            public ObservableValue<Boolean> call(CellDataFeatures<Spot, Boolean> param) {
                return param.getValue().getRoadtrain();
            }
        });

roadtrainCol.setCellFactory(CheckBoxTableCell.forTableColumn(roadtrainCol));

ChangeListener<Boolean> roadTrainChangeListener = (observable, oldValue, newValue) -> {
    Spot spot = (Spot) ((ReadOnlyProperty) observable).getBean();
    System.out.println("del");
    rettedeSpots(spot.getId());
};

// add/remove listeners when Spots are added to/removed from the list
tableViewId.getItems().addListener((ListChangeListener.Change<? extends Spot> c) -> {
    while (c.next()) {
        for (Spot s : c.getRemoved()) {
            s.getRoadtrain().removeListener(roadTrainChangeListener);
        }
        for (Spot s : c.getAddedSubList()) {
            s.getRoadtrain().addListener(roadTrainChangeListener);
        }
    }
});

// add listeners to items already in the list
for (Spot s : tableViewId.getItems()) {
    s.getRoadtrain().addListener(roadTrainChangeListener);
}

Note that this only works, if you assigned the Spot instance containing the SimpleBooleanProperty as bean , eg

Spot() {
     this.roadtrain = new SimpleBooleanProperty(this, "roadtrain");
}

or

Spot(boolean roadtrain) {
     this.roadtrain = new SimpleBooleanProperty(this, "roadtrain", roadtrain);
}

I just used the bean to avoid having tho create new listeners for every item.

Also consider following the naming conventions for JavaFX properties:

  • Property getter is named <propertyName>Property (not get<capitalFirstLetterPropertyName> )
  • the getter for the property's value is named get<capitalFirstLetterPropertyName>
  • the setter for the property's value is named set<capitalFirstLetterPropertyName>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM