简体   繁体   English

如何知道tableColumn中复选框的值在javafx中是否已更改

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

hi I created a checkbox in tablecolumn: 嗨,我在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 : Javadocs中获取CheckBoxTableCell

Note that the CheckBoxTableCell renders the CheckBox 'live', meaning that the CheckBox is always interactive and can be directly toggled by the user. 请注意,CheckBoxTableCell呈现“实时”的CheckBox,这意味着CheckBox始终是交互式的,并且可以由用户直接切换。 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. 如果希望收到有关更改的通知,建议直接观察由CheckBox操纵的布尔属性。

Assuming your table model class Information has a property accessor method for the orientation property, ie 假设您的表模型类Information具有orientation属性的属性访问器方法,即

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 它可以工作,但是在我的表中有2列带有复选框(orientation,ist)。当我收到消息时,如何知道2列中的哪一列发生了更改

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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