简体   繁体   中英

How to know if the value of a checkbox in a tableColumn had change in javafx

hi I created a checkbox in tablecolumn:

  col_orien.setCellValueFactory(new PropertyValueFactory<Information, Boolean>("orientation")); col_orien.setCellFactory(CheckBoxTableCell.forTableColumn(col_orien)); col_orien.setEditable(true); col_orien.setOnEditCommit(new EventHandler<CellEditEvent<Information,Boolean>>() { @Override public void handle(CellEditEvent<Information, Boolean> event) { System.out.println("Edit commit"); } }); 

The problem is when I changed the value of the checkbox the message didn't appear

From the Javadocs for CheckBoxTableCell :

Note that the CheckBoxTableCell renders the CheckBox 'live', meaning that the CheckBox is always interactive and can be directly toggled by the user. This means that it is not necessary that the cell enter its editing state (usually by the user double-clicking on the cell). A side-effect of this is that the usual editing callbacks (such as on edit commit) will not be called. If you want to be notified of changes, it is recommended to directly observe the boolean properties that are manipulated by the CheckBox.

Assuming your table model class Information has a property accessor method for the orientation property, ie

public class Information {
    // ...

    public BooleanProperty orientationProperty() {
        // ...
    }

    // ...
}

then the orientation property of the relevant object will be updated automatically when the check box is selected and de-selected. Hence all you need to do is listen for changes on those properties themselves:

Information info = new Information(...);
table.getItems().add(info);
info.orientationProperty().addListener((obs, oldValue, newValue) 
    -> System.out.println("orientation property edited"));

i use this solution :

  ObservableList<Information> data = FXCollections.<Information>observableArrayList( new Callback<Information, Observable[]>() { @Override public Observable[] call(Information inf) { return new Observable[]{inf.orientationProperty(),inf.istProperty()}; } } ); data.addListener(new ListChangeListener<Information>() { @Override public void onChanged( javafx.collections.ListChangeListener.Change<? extends Information> change) { System.out.println("List changed"); while (change.next()) { if (change.wasUpdated()) { System.out.println("List updated"); System.out.println(change.getAddedSubList()); } } } }); 
And it's work, but in my table there is 2 columns with check box(orientation ,ist).how can I know which of the 2 columns had change, when I get the message

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