简体   繁体   English

JavaFX TableCell可根据行值进行编辑

[英]JavaFX TableCell editable depending on row value

I have a TableView with multiple columns and I want some of them to be editable only if a value is verified on another column of the same row. 我有一个包含多列的TableView ,并且我希望其中的一些仅在对同一行的另一列进行验证后才可编辑。 In other words, I want the "editable" property of the TableCell to be row-specific instead of column-specific. 换句话说,我希望TableCell的“ editable”属性是特定于行的,而不是特定于列的。

The real problem is I can't find any way to get the TableCell object from the column, and thus I am unable to set its editable property to false. 真正的问题是我找不到从列中获取TableCell对象的任何方法,因此我无法将其editable属性设置为false。

Could you help me do it? 你能帮我吗? Is it even possible with JavaFX? JavaFX甚至可能吗?


For example, this would be the column in where to check the "editable" property: 例如,这将是检查“可编辑”属性的列:

nextIsEditableCol.setCellValueFactory(new PropertyValueFactory<Person, Boolean>("nextIsEditable"));
nextIsEditableCol.setCellFactory(ChoiceBoxTableCell.<Person,Boolean>forTableColumn(true,false));
nextIsEditableCol.setOnEditCommit(new EventHandler<CellEditEvent<Person, Boolean>>() {
    @Override
    public void handle(CellEditEvent<Person, Boolean> t) {
        if(t.getNewValue()){
            //Here be the code that sets the next cell of this row to editable
        }
    }
});

And this would be the column that may or may not be editable: 这将是可编辑或不可编辑的列:

mayBeEditedCol.setCellValueFactory(new PropertyValueFactory<Person, String>("name"));
mayBeEditedCol.setCellFactory(TextFieldTableCell.forTableColumn());
nextIsEditableCol.setOnEditCommit(new EventHandler<CellEditEvent<Person, String>>() {
    @Override
    public void handle(CellEditEvent<Person, Boolean> t) {
        t.getRowValue().setName(t.getNewValue);
    }
});

You say you want it to be row specific instead of column specific? 您说要使其特定于行而不是特定于列?

You could use a cell change listener. 您可以使用单元格更改侦听器。 Whenever a cell is changed you would get the x,y coords of the cell. 无论何时更改单元格,您都会获得该单元格的x,y坐标。 You could create an array of booleans like isColumnDependent . 您可以创建一个像isColumnDependent这样的布尔数组。 If the value is true, you could then determine the number of columns, and create a method that iterates through the cells to the left and right of it disabling the editablility of them if the desired rule is not met. 如果该值为true,则可以确定列数,并创建一种方法,该方法在单元格的左侧和右侧进行迭代,如果不满足所需的规则,则禁用它们的可编辑性。

Just an idea, haven't worked with JavaFX much. 只是一个想法,与JavaFX的合作并不多。

If it is anything like JTable's setup, you could create a more row oriented table model. 如果类似于JTable的设置,则可以创建一个更加面向行的表模型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM