【问题标题】:How to disable a table cell for editing in JavaFX 2如何在 JavaFX 2 中禁用表格单元格进行编辑
【发布时间】:2013-02-01 17:02:37
【问题描述】:

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

TableModel.isCellEditable(row, col)

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

我设法编写的代码是:

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

};

问题是没有视觉线索表明某些项目被禁用以进行编辑。

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

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
                }


            };
        }

    });

【问题讨论】:

  • 您是否尝试实现自定义可编辑单元格?您将不得不覆盖某些方法,例如 startEdit、cancelEDit、updateItem,您将能够在其中做出决定、提供编辑或不进行编辑的可能性。 @Override public void startEdit() { super.startEdit(); if (textField == null) { createTextField(); } 设置文本(空);设置图形(文本字段); // 这里 textField.setText(getString()); textField.requestFocus(); }
  • 另外,您可以使用样式类来显示该单元格不可编辑:在单元格中,跟踪一个值,并据此更改样式类分配。
  • 我在问题中添加了带有单元格工厂的代码,但是我在updateItem方法中只有一个单元格值,我需要一些其他单元格值来确定单元格是否可编辑。
  • 你的意思是,一些单元格想知道一些关于其他单元格值的附加信息?
  • 没错,他们需要在同一行的另一个单元格中显示域对象的某些字段。

标签: javafx-2 javafx


【解决方案1】:

好像

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

可在单元格中完成这项工作。 完整的方法如下:

@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");
        }
    }
}

【讨论】:

  • 这是什么覆盖?这个方法在什么类中定义?
  • 嗨,这是 TextFieldTableCell.updateItem 的主体,如问题所述
猜你喜欢
  • 2014-09-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-05
  • 2020-10-28
  • 2014-02-22
  • 2013-10-20
相关资源
最近更新 更多