2017-07-12 57 views
0

我有一個布爾值屬性,指示是否有該對象相應的文件:綁定的TableView行背景顏色布爾物業

public Track { 
    BooleanProperty fileIsMissing = new SimpleBooleanProperty (false); 
    ... 
    public BooleanProperty fileIsMissingProperty() { 
     return fileIsMissing; 
    } 

    public boolean isMissingFile() { 
     return fileIsMissing.getValue(); 
    } 
    ... 
} 

的這個值被另一個線程,如果該文件被刪除或添加更新。

我有一個TableView顯示行中的軌道。

我想有行變化的背景顏色時BooleanProperty變化 - 如果它是true,我想爲背景爲紅色,如果是false我想它是正常的彩色。

我有以下,這幾乎工程:

trackTable.setRowFactory(tv -> { 
    TableRow <CurrentListTrack> row = new TableRow <>(); 

    row.itemProperty().addListener((obs, oldValue, newValue) -> { 
     if (newValue.isMissingFile()) { 
      row.getStyleClass().add("file-missing"); 
     } else { 
      row.getStyleClass().removeAll(Collections.singleton("file-missing")); 
     } 
    }); 

    ... 
} 

而且在CSS:

.file-missing { 
    -fx-control-inner-background: palevioletred; 
} 

此設置背景的情況下正確的軌道,首先添加到表中,但它不」 t更新爲價值fileIsMissing的變化,這是有道理的,因爲我根本不具約束力。

我該如何做到這一點?

回答

2

也許你需要聽你的fileIsMissing屬性。

當前您的偵聽器將對添加的新的Track記錄做出反應,嵌套的偵聽器將對Track更改作出反應。

可能是這樣的:

row.itemProperty().addListener((obs, oldValue, newTrackValue) -> { 
    if (newTrackValue != null) { 
     newTrackValue.fileIsMissingProperty().addListener((o, old, newValue) -> { 
     if (newValue) { 
      row.getStyleClass().add("file-missing"); 
     } else { 
      row.getStyleClass().remove("file-missing"); 
     } 
    } 
}); 

BTW,刪除一個列表元素你可以使用list.remove(對象),而通過的removeAll名單。

+0

這樣做的伎倆,謝謝! – JoshuaD