2017-01-18 3 views
0

Tableviewでさまざまな状態を数え、ラベルに表示したいとします。値が変更されるたびにリスト全体を反復することなくこの機能を実現する方法はありますか?異なる状態の量をラベルにバインドする方法はありますか?反復せずにTableviewの異なる文字列のJavaFX数の出現?

は現在、私は、リストを反復処理でこれをやっている:

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); 

} 

EDIT:
リスト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; 
} 
} 
+0

をあなたは、ストリーム(ラムダ)でそれを行うことができますが、でも、この方法は、反復を使用しています「**私は、どのような場合で考える**」: 'liste.stream()。filter(f-> filtering).forEach(i-> doAction);' –

+0

リストが 'ObservableList'ならば(もしあなたがそれを作ることができるなら)、それを観察して見ることができます何が変わったのかしかし、本当にあなたはリストが何であるか、そしてそれが答えることができるために 'StringContainer'などが何であるかを教えなければなりません。 –

+0

はい、そのObservableListです。投稿を「StringContainer」の情報で更新しました – Ronon

答えて

1

あなたは、次の線に沿って何かを行うことができます。

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) ; 

      } 
     } 
    } 
}); 
関連する問題