You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JavaFX实现提交编辑后禁用指定TableCell(仅允许编辑一次)

解决TableView单个单元格编辑一次后禁用的问题

要实现quantity列的单元格仅允许编辑一次,核心思路是给每个Item对象添加一个标记属性,记录该单元格是否已完成编辑,然后在自定义单元格类中根据这个标记控制编辑权限。以下是具体实现步骤:

1. 修改Item类,添加编辑状态标记

我们需要给Item增加一个BooleanProperty来跟踪quantity列的编辑状态:

public class Item {
    // 原有属性保持不变
    private final IntegerProperty id;
    private final ObjectProperty<LocalDate> weekDate;
    private final ObjectProperty<LocalTime> time;
    private final StringProperty quantity;
    // 新增:标记quantity是否已编辑提交
    private final BooleanProperty quantityEdited;

    public Item() {
        this(0, null, null, null);
    }

    public Item(int id, LocalDate weekDate, LocalTime time, String quantity) {
        this.id = new SimpleIntegerProperty(id);
        this.weekDate = new SimpleObjectProperty(weekDate);
        this.time = new SimpleObjectProperty(time);
        this.quantity = new SimpleStringProperty(quantity);
        // 默认未编辑
        this.quantityEdited = new SimpleBooleanProperty(false);
    }

    // 新增getter、setter和property方法
    public boolean isQuantityEdited() {
        return quantityEdited.get();
    }

    public void setQuantityEdited(boolean quantityEdited) {
        this.quantityEdited.set(quantityEdited);
    }

    public BooleanProperty quantityEditedProperty() {
        return quantityEdited;
    }

    // 原有属性的getter、setter和property方法保持不变
    // ...(此处省略原有代码)
}

2. 修改自定义单元格JFXTextFieldCell,控制编辑权限

在JFXTextFieldCell中,重写startEdit方法,检查当前行对应的Item的quantityEdited状态,如果已经编辑过,就阻止进入编辑状态:

public class JFXTextFieldCell extends TableCell<Item, String> {
    private JFXTextField textField;

    public JFXTextFieldCell() {
    }

    @Override
    public void startEdit() {
        // 获取当前行的Item对象
        Item item = getTableView().getItems().get(getIndex());
        // 如果quantity已经编辑过,不允许进入编辑状态
        if (item != null && item.isQuantityEdited()) {
            return;
        }
        // 否则执行原有编辑逻辑
        if (!isEmpty()) {
            super.startEdit();
            createTextField();
            setText(null);
            setGraphic(textField);
            textField.selectAll();
        }
    }

    // 原有方法(cancelEdit、updateItem、createTextField、getString)保持不变
    // ...(此处省略原有代码)
}

3. 在quantityColumn的编辑提交事件中标记状态

回到TableViewController的quantityColumn.setOnEditCommit方法,提交编辑后将对应Item的quantityEdited设为true:

quantityColumn.setOnEditCommit(event -> {
    Item user = event.getRowValue();
    user.setQuantity(event.getNewValue());
    updateQuantity("Quantity", event.getNewValue(), user.getID());
    // 标记该单元格已编辑,后续无法再编辑
    user.setQuantityEdited(true);
    // 可选:更新单元格显示,让用户直观看到已锁定
    table.refresh();
});

4. 可选:添加视觉提示

为了让用户更直观地知道单元格已锁定,可以在JFXTextFieldCell的updateItem方法中添加样式:

@Override
public void updateItem(String item, boolean empty) {
    super.updateItem(item, empty);
    if (empty) {
        setText(item);
        setGraphic(null);
        setStyle("");
    } else {
        Item rowItem = getTableView().getItems().get(getIndex());
        if (rowItem != null && rowItem.isQuantityEdited()) {
            // 添加锁定样式,比如灰色背景
            setStyle("-fx-background-color: #f0f0f0;");
            setText(getString());
            setGraphic(null);
        } else {
            setStyle("");
            if (isEditing()) {
                if (textField != null) {
                    textField.setText(getString());
                }
                setText(null);
                setGraphic(textField);
            } else {
                setText(getString());
                setGraphic(null);
            }
        }
    }
}

这样修改后,每个quantity列的单元格在第一次编辑提交后,就会被标记为已编辑,无法再次进入编辑状态,同时不会影响其他单元格或整列的编辑权限。

内容的提问来源于stack exchange,提问作者Kay

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 18:42:43