如何从TableView获取行?

时间:2019-03-05 18:36:10

标签: java for-loop colors tableview

我正在创建一个大厅应用程序,它将在java tableview上显示所有组。用户可以加入其中有空间的群组,否则他们将无法加入。

我已经能够创建它,但是我希望能够用绿色将其中有空格的组的行涂成绿色,将已满的组涂成红色。

我将在下面提供我的代码。我收到NullPointerException,我不知道为什么。谢谢。

private void visualGroupAvailability() {
        boolean isThereSpace;

       for (currentGroupsModel o : groupsTable.getItems()) {
           TableRow<currentGroupsModel> currentRow = getTableRow(o.getGroupID());
             int limit = o.getNumberOfUsers();
             isThereSpace = checkSpaceInGroup(o);
             if(isThereSpace) {
                  currentRow.setStyle("-fx-background-color: #" + "388e3c ");
             } else {
                 currentRow.setStyle("-fx-background-color: #" + "ffcdd2 ");
             }
        }
    }

private TableRow<currentGroupsModel> getTableRow(int rowIndex) {
    Set<Node> tableRowCell = groupsTable.lookupAll(".table-row-cell");
    TableRow<currentGroupsModel> row = null;
    for (Node tableRow : tableRowCell) {
        TableRow<currentGroupsModel> r = (TableRow<currentGroupsModel>) tableRow;
            row = r;  
    }
    return row;
}

公共类currentGroupsModel {

字符串groupName,groupDescription,hostName,groupType; 整数numberOfUsers,groupID;

public currentGroupsModel(String gName, String gDesc, String hostName, String groupType, Integer numberOfUsers, Integer groupID){
    this.groupName = gName;
    this.groupDescription = gDesc;
    this.hostName = hostName;
    this.groupType = groupType;
    this.numberOfUsers = numberOfUsers;
    this.groupID = groupID;
}

public String getGroupName() {
    return groupName;
}

public void setGroupName(String groupName) {
    this.groupName = groupName;
}

public String getGroupDescription() {
    return groupDescription;
}

public void setGroupDescription(String groupDescription) {
    this.groupDescription = groupDescription;
}

public String getHostName() {
    return hostName;
}

public void setHostName(String hostName) {
    this.hostName = hostName;
}

public String getGroupType() {
    return groupType;
}

public void setGroupType(String groupType) {
    this.groupType = groupType;
}

public Integer getNumberOfUsers() {
    return numberOfUsers;
}

public void setNumberOfUsers(int numberOfUsers) {
    this.numberOfUsers = numberOfUsers;
}

public Integer getGroupID(){
    return this.groupID;
}

public void setGroupID(Integer newID){
    this.groupID = newID;
}

}

1 个答案:

答案 0 :(得分:0)

您给我们的东西并不能真正回答这个问题。如果存在一个我们不知道的currentGroupModel,并且一直存在红色毛发,则很难找到NullPointerException。例如,为什么您要限制存储一些东西,却从未使用呢!为什么要向getTableRow传递从未使用过的rowIndex?就我所知,您的getTableRow返回表中的最后一个TableRow,而不是特定的。在最终提供一些代码以了解currentGroupModel的内部工作方式之前,请先考虑解决这些问题。

相关问题