gpt4 book ai didi

java - 如何使我的 CheckBoxTableCell 在 Javafx 中可编辑?

转载 作者:行者123 更新时间:2023-12-02 03:44:39 25 4
gpt4 key购买 nike

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();
}
}

最佳答案

要使 TableCell 可编辑,您需要设置 TableView.editable property也为true

此外,您在 cellValueFactory 中创建一个新属性,这意味着编辑的值将不是 TableView 项的属性,而是新属性(property);您的元素的值(value)不会改变。

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;
}

关于java - 如何使我的 CheckBoxTableCell 在 Javafx 中可编辑?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36414735/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com