isText.setEditable(true);
isText.setCellValueFactory(new Callback<CellDataFeatures<TableItem, Boolean>, ObservableValue<Boolean>>() {
@Override
public ObservableValue<Boolean> call(CellDataFeatures<TableItem, Boolean> p) {
return new SimpleBooleanProperty(p.getValue().getIsText());
}
});
isText.setCellFactory(new Callback<TableColumn<TableItem, Boolean>, TableCell<TableItem, Boolean>>() {
@Override
public TableCell<TableItem, Boolean> call(TableColumn<TableItem, Boolean> p) {
return new CheckBoxTableCell<>();
}
});
这是我的代码,我真的想检查并取消选中该框,并通过我在下面定义的方法更改原始数据,
/**
* Set the boolean value of if this variable is viewed as text
*/
public void setIsText(boolean newVal) {
isText.set(newVal);
}
这是我对TableItem
,
public class TableItem {
private final SimpleIntegerProperty index;
private final SimpleStringProperty variable;
private final SimpleBooleanProperty isText;
/**
* Constructor for TableItem
*
* @param index
* index of the variable
* @param variable
* variable name
* @param isText
* initial boolean value of if it is viewed as a text or not
*/
public TableItem (int index, String variable, boolean isText) {
this.index = new SimpleIntegerProperty(index);
this.variable = new SimpleStringProperty(variable);
this.isText = new SimpleBooleanProperty(isText);
}
/**
* Get the boolean value of if this variable is viewed as text
* @return
* the boolean value notifies if the variable is text or not
*/
public boolean getIsText() {
return isText.get();
}
/**
* Set the boolean value of if this variable is viewed as text
*/
public void setIsText(boolean newVal) {
isText.set(newVal);
}
/**
* Get the index of the variable
*
* @return
* the index
*/
public int getIndex() {
return index.get();
}
/**
* Get the string representation of the variable
*
* @return
* the string of the variable
*/
public String getVariable() {
return variable.get();
}
}
答案 0 :(得分:2)
要使TableCell
可编辑,您还需要将TableView.editable
property设置为true
。
此外,您在cellValueFactory
中创建新属性,这意味着编辑的值不是TableView
项的属性,而是新属性;您的商品的价值不会改变。
将cellValueFactory
更改为
new Callback<CellDataFeatures<TableItem, Boolean>, ObservableValue<Boolean>>() {
@Override
public ObservableValue<Boolean> call(CellDataFeatures<TableItem, Boolean> p) {
return p.getValue().isTextProperty();
}
}
并将以下属性getter添加到TableItem
public BooleanProperty isTextProperty() {
return this.isText;
}