gpt4 book ai didi

JavaFX 计算 Tableview 中不同字符串的出现而不进行迭代?

转载 作者:行者123 更新时间:2023-12-01 09:00:22 24 4
gpt4 key购买 nike

我想计算 Tableview 中的不同状态并将其显示在标签中。有没有一种方法可以实现此功能,而无需每次值更改时迭代整个列表?有没有办法将不同状态的数量绑定(bind)到标签上?

目前我正在通过迭代列表来做到这一点:

private void updateLbl(){
int on = 0, off = 0, un = 0;
for(StringContainer wps : streamLinkList){
if(wps.getState().equals("online")){
on++;
} else if(wps.getState().equals("offline")){
off++;
} else if(wps.getState().equals("unknown")){
un++;
} else {
se++;
}
}
onLbl.setText("Online: " + on);
offLbl.setText("Offline: " + off);
unLbl.setText("Unknown: " + un);

}

编辑:
该列表是一个 ObservableList。 StringContainer 包含不同的 StringProperty。

class StringContainer{
private StringProperty state;

public StringContainer(){
this.state = new SimpleStringProperty();
}
// state
public void setState(String state){
this.state.set(state);
}

public String getState(){
return state.get();
}

public StringProperty stateProperty(){
return state;
}
}

最佳答案

您可以按照以下方式执行操作:

ObservableList<StringContainer> items = table.getItems();

// initialize counts (only needed once, and only if items is non-empty):
int on = off = unk = 0 ;
for (StringContainer item : items) {
if ("online".equals(item.getState())) on++ ;
if ("offline".equals(item.getState())) off++ ;
if ("unknown".equals(item.getState())) unk++ ;
}

IntegerProperty onCount = new SimpleIntegerProperty(on);
IntegerProperty offCount = new SimpleIntegerProperty(off);
IntegerProperty unknownCount = new SimpleIntegerProperty(unk);

onLbl.textProperty().bind(onCount.asString("Online: %s"));
offLbl.textProperty().bind(offCount.asString("Offline: %s"));
unLbl.textProperty().bind(unknownCount.asString("Unknown: %s"));

ChangeListener<String> listener = (obs, oldValue, newValue) -> {
if ("online".equals(oldValue)) onCount.set(onCount.get() - 1);
if ("offline".equals(oldValue)) offCount.set(offCount.get() - 1);
if ("unknown".equals(oldValue)) unknownCount.set(unknownCount.get() - 1);
if ("online".equals(newValue)) onCount.set(onCount.get() + 1);
if ("offline".equals(newValue)) offCount.set(offCount.get() + 1);
if ("unknown".equals(newValue)) unknownCount.set(unknownCount.get() + 1);
};

items.forEach(item -> item.stateProperty().addListener(listener));

items.addListener((ListChangeListener.Change<? extends StringContainer> c) -> {
while (c.next()) {
if (c.wasAdded()) {
for (StringContainer item : c.getAddedSubList()) {
item.stateProperty().addListener(listener);
if ("online".equals(item.getState())) onCount.set(onCount.get() + 1) ;
if ("offline".equals(item.getState())) offCount.set(offCount.get() + 1) ;
if ("unknown".equals(item.getState())) unknownCount.set(unknownCount.get() + 1) ;

}
}
if (c.wasRemoved()) {
for (StringContainer item : c.getRemoved()) {
item.stateProperty().removeListener(listener);
if ("online".equals(item.getState())) onCount.set(onCount.get() - 1) ;
if ("offline".equals(item.getState())) offCount.set(offCount.get() - 1) ;
if ("unknown".equals(item.getState())) unknownCount.set(unknownCount.get() - 1) ;

}
}
}
});

关于JavaFX 计算 Tableview 中不同字符串的出现而不进行迭代?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41722681/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com