1

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

TableModel.isCellEditable(row, col)

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

The code I managed to write is:

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
                }


            };
        }

    });
Lesiak
  • 22,088
  • 2
  • 41
  • 65
  • Did you try to implement a custom editable cell? You will have to override some methods, like startEdit, cancelEDit, updateItem, where you will be able to make a decision, give a possibility to edit, or don't. @Override public void startEdit() { super.startEdit(); if (textField == null) { createTextField(); } setText(null); setGraphic(textField); // HERE textField.setText(getString()); textField.requestFocus(); } – Alexander Kirov Feb 01 '13 at 23:21
  • Also, you can use style classes, to show, that cell is not editable: in a cell, track a value, and change style class assignation according to that. – Alexander Kirov Feb 01 '13 at 23:24
  • I added the code with cell factory in the question, but I have only one cell value in updateItem method, and I need some other cells values to determine whether the cell is editable. – Lesiak Feb 06 '13 at 11:52
  • Do you mean, that some cells want to know some additional information about other cells' values? – Alexander Kirov Feb 06 '13 at 19:56
  • Exactly, they need some fields of the domain object that are displayed in another cells in the same row. – Lesiak Feb 07 '13 at 16:51

1 Answers1

2

It seems that

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");
        }
    }
}
Lesiak
  • 22,088
  • 2
  • 41
  • 65