如何禁用表格单元格以便在JavaFX 2中进行编辑 [英] How to disable a table cell for editing in JavaFX 2

查看:89
本文介绍了如何禁用表格单元格以便在JavaFX 2中进行编辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在JavaFX 2中,我想禁用用于编辑的列中的某些单元格.类似于

In JavaFX 2, I want to disable a some cells in a column for edition. Something along the lines of

TableModel.isCellEditable(row, col)

在Swing中.我需要该行中的项目来确定单元格是否可编辑,单元格值不够.

in Swing. I need the item in the row to make a decision whether the cell is editable, cell value is not enough.

我设法编写的代码是:

TableView<FilterItem> localTableView = new TableView<FilterItem>() {
    public void edit(int paramInt, TableColumn<FilterItem, ?> paramTableColumn) {
        if (paramInt >= 0) {
            FilterItem item = getItems().get(paramInt); //get item in the row
                if (!item.isPropagated()) {             //condition
                    return;
                }
        }
        super.edit(paramInt, paramTableColumn);
    }

};

问题在于,没有视觉线索表明某些项目被禁用进行编辑.

The problem is that there is no visual clue that certain items are disabled for editing.

设置单元工厂是我尝试的第一件事,但是我无法访问更新单元方法中的行数据:

Setting the cell factory was the firs thing I tried, but I have no access to row data in update cell method:

localValueCol.setCellFactory(
    new Callback<TableColumn<FilterItem, String>, TableCell<FilterItem, String>>() {

        @Override
        public TableCell<FilterItem, String> call(TableColumn<FilterItem, String> paramTableColumn) {
            return new TextFieldTableCell<FilterItem, String>() {
                @Override
                public void updateItem(String s, boolean b) {
                    super.updateItem(s, b);
                    // it is possible set editable property here, 
                    // but other cells are not available to make a decision
                }


            };
        }

    });

推荐答案

似乎

TableRow row = getTableRow();
FilterItem item = (FilterItem) row.getItem();

单元格中可用的

做这项工作. 完整的方法如下:

available in the cell's does the job. The complete method is as follows:

@Override
public void updateItem(T t, boolean empty) {
    super.updateItem(t, empty);
    TableRow row = getTableRow();
    if (row != null) {
        FilterItem item = (FilterItem) row.getItem();
        //Test for disable condition
        if (item != null && item.getRating() < 10) {
            setDisable(true);
            setEditable(false);
            this.setStyle("-fx-text-fill: grey");
        }
    }
}

这篇关于如何禁用表格单元格以便在JavaFX 2中进行编辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆