问题
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
}
};
}
});
回答1:
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");
}
}
}
来源:https://stackoverflow.com/questions/14651369/how-to-disable-a-table-cell-for-editing-in-javafx-2