Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在CellFactory的updateItem()中获取行值 ageColumn.setCellFactory(参数->新建TableCell(){ @凌驾 受保护的void updateItem(字符串项,布尔值为空){ 参数getCellFactory()。 super.updateItem(项,空); setText(空?null:String.valueOf(item)); if(person.getName.equals(“MATEUS”)){ setStyle(“-fx背景色:红色;”); } } });_Java_Javafx - Fatal编程技术网

Java 如何在CellFactory的updateItem()中获取行值 ageColumn.setCellFactory(参数->新建TableCell(){ @凌驾 受保护的void updateItem(字符串项,布尔值为空){ 参数getCellFactory()。 super.updateItem(项,空); setText(空?null:String.valueOf(item)); if(person.getName.equals(“MATEUS”)){ setStyle(“-fx背景色:红色;”); } } });

Java 如何在CellFactory的updateItem()中获取行值 ageColumn.setCellFactory(参数->新建TableCell(){ @凌驾 受保护的void updateItem(字符串项,布尔值为空){ 参数getCellFactory()。 super.updateItem(项,空); setText(空?null:String.valueOf(item)); if(person.getName.equals(“MATEUS”)){ setStyle(“-fx背景色:红色;”); } } });,java,javafx,Java,Javafx,如何从表中获取行值“Person”?我只能从单元格中获取值,但不能从整个对象中获取值。您可以这样做 ageColumn.setCellFactory(param -> new TableCell<Person, String>(){ @Override protected void updateItem(String item, boolean empty) { param.getCellFactory(). super.upda

如何从表中获取行值“Person”?我只能从单元格中获取值,但不能从整个对象中获取值。

您可以这样做

ageColumn.setCellFactory(param -> new TableCell<Person, String>(){
    @Override
    protected void updateItem(String item, boolean empty) {
        param.getCellFactory().
        super.updateItem(item, empty);
        setText(empty ? null : String.valueOf(item));

        if(person.getName.equals("MATEUS")) {
            setStyle("-fx-background-color: red;");
        }
    }
});
你也可以

Person person = getTableView().getItems().get(getIndex());
但这不太理想(在我看来),因为
getTableRow()
返回一个原始类型,因此它需要未检查的向下转换

显然,只有当
empty
false
时,这两个选项中的任何一个才有效,因此它们应该在检查中:

Person person = (Person) getTableRow().getItem();
ageColumn.setCellFactory(参数->新建TableCell(){
@凌驾
受保护的void updateItem(字符串项,布尔值为空){
super.updateItem(项,空);
if(空){
setText(空);
设置样式(“”);
}否则{
setText(项目);
Person=getTableView().getItems().get(getIndex());
if(person.getName.equals(“MATEUS”)){
setStyle(“-fx背景色:红色;”);
}否则{
设置样式(“”);
}
}
}
});

getTableRow().getItem()
有时返回null,而
getTableView().getItems().get(getIndex())
返回预期的对象。我的案例是在Kotlin的
updateItem(..)
中。@BingLi224除非您将空值显式地放入表的项列表中,
getTableRow()。getItem()
在非空单元格中不会返回空值。
ageColumn.setCellFactory(param -> new TableCell<Person, String>(){
    @Override
    protected void updateItem(String item, boolean empty) {
        super.updateItem(item, empty);
        if (empty) {
            setText(null);
            setStyle("");
        } else {
            setText(item);
            Person person = getTableView().getItems().get(getIndex());
            if(person.getName.equals("MATEUS")) {
                setStyle("-fx-background-color: red;");
            } else {
                setStyle("");
            }
        }
    }
});