简体   繁体   English

获取具有某些条件的tableView项目的特定行(JavaFX)

[英]Get particular row of tableView item which has some condition (JavaFX)

I am making attendance TableView for one person. 我正在为一个人制作出勤表。 I am getting tableView row and if person in that specific day has status "present" (string) then I want to change that particular row and change its background-color to green. 我正在获取tableView行,并且如果特定日期的人的状态为“存在”(字符串),那么我想更改该特定行并将其背景颜色更改为绿色。

Then if he is "absent" in that particular day I want to change that row and set red background. 然后,如果他在该特定日期“不在”,我想更改该行并设置红色背景。

Otherwise if there is no date and status in row I need to change row again and set white background. 否则,如果行中没有日期和状态,则需要再次更改行并设置白色背景。

PROBLEM: It is going through first if statement and then set every row green because first statement is "true". 问题:首先要通过if语句,然后将每行设置为绿色,因为第一个语句为“ true”。

I want to get only rows with present and set only that rows to green background colour. 我只想获取存在的行,并将该行设置为绿色背景色。

*getStatus returns String * getStatus返回字符串

studentTable.setRowFactory(tableView -> {
    final TableRow<Attendance> row = new TableRow<>();

        for (Attendance item : studentTable.getItems()) {
            if (item.getStatus().equals("present")) {
                row.setStyle("-fx-background-color:green;");
            }
            else if (item.getStatus().equals("absent")){
                row.setStyle("-fx-background-color:red;");
            }
            else {
                row.setStyle("-fx-background-color:white;");
            }
        }
return row;
    });
}

PICTURE WITH RESULT: Image 结果的图片图片

I will be thankful for any solution and any help :) 我将感谢您提供任何解决方案和帮助:)

All your code does is create a TableRow when needed, and then each time a TableRow is created, it iterates over all the items in the table, continually changing the style of that one single row . 你所有的代码不会是创建TableRow需要的时候,再每一次TableRow被创建,它遍历表中的所有项目,不断变化的一个单列的风格。 So the row will simply have the style determined by the last item in the table's items list (because that item determines the final call to setStyle(...) ). 因此,该行将仅具有由表的项目列表中的最后一个项目确定的样式(因为该项目确定对setStyle(...)的最终调用)。 And of course, you do this for every row you create, so unsurprisingly every row has the same style. 当然,您对创建的每一行都执行此操作,因此,毫无疑问,每一行都有相同的样式。

The TableRow 's updateItem(...) method is called when the row is first used to display an Attendance instance, and again any time the row is reused to display a different Attendance instance. 当该行首次用于显示Attendance实例时,以及每当该行被重用于显示不同的Attendance实例时,都会调用TableRowupdateItem(...)方法。 Thus you need to override updateItem() and place the logic there. 因此,您需要重写updateItem()并将逻辑放置在那里。

studentTable.setRowFactory(tableView -> new TableRow<Attendance>() {
    @Override
    protected void updateItem(Attendance attendance, boolean empty) {
        super.updateItem(attendance, empty);
        if (empty) {
            setStyle("");
        } else if (attendance.getStatus().equals("present")) {
            setStyle("-fx-background-color:green;");
        } else if (attendance.getStatus().equals("absent")) {
            setStyle("-fx-background-color:red;");
        } else {
            setStyle("-fx-background-color:white;");
        }
    }
});

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

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