本文介绍了JavaFX在Tableview中计算不同字符串的外观而不进行迭代?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to count different states in the Tableview and display it in Labels. Is there a way to achieve this functionality without iterating over the whole list every time a value changes? Is there a way to bind the amount of the different states to a label?

目前我正在迭代List上这样做:

Currently I'm doing this with iterating over the List:

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包含不同的StringPropertys。


The List is an ObservableList. StringContainer contains different StringPropertys.

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中计算不同字符串的外观而不进行迭代?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 16:21