2012-06-16 41 views
2

有什麼方法可以在列表視圖中更改選擇欄文本顏色? 最好使用CSS。在TableView中,您可以使用:Javafx ListView選擇欄使用CellFactory時的文本顏色

-fx-selection-bar-text: white; 

但是,這對ListView不起作用。

更新:上述情況發生在使用CellFactories呈現單元格時。

lvRooms.setCellFactory(new Callback<ListView<String>, ListCell<String>>() { 
     @Override public ListCell<String> call(ListView<String> list) { 
      return new RoomCell(); 
     } 
    }); 

在Cell Factory類中,我很樂意介紹選擇行時的情況。

但是:它只在開始時調用一次,並非每次移動選擇欄,因此isSelected()方法總是呈現false。

UPDATE 2:這是RoomCell實現:

class RoomCell extends ListCell<String> { 
     @Override 
     public void updateItem(String item, boolean empty) { 
      super.updateItem(item, empty); 

      if (item != null) { 
       Log.debug("RoomCell called, item: "+item); 
       final Label lbl = new Label(item); // The room name will be displayed here 
       lbl.setFont(Font.font("Segoe UI", FontWeight.BOLD, 18)); 
       lbl.setStyle("-fx-text-fill: black"); 

       //lbl.setTextFill(isSelected()?Color.WHITE: Color.BLACK); 
       if (isSelected()) // This is always false :(
        lbl.setStyle("-fx-text-fill: yellow"); 

       if (Rooms.getBoolean(item, "OwnerStatus")) { 
        lbl.setEffect(new DropShadow(15, Color.BLUEVIOLET)); 
        lbl.setGraphic(new ImageView(
            new Image(getClass().getResourceAsStream("images/universal.png")))); 
       } else { 
        lbl.setGraphic(new ImageView(
            new Image(getClass().getResourceAsStream("images/yin-yang.png")))); 
        lbl.setEffect(new DropShadow(15, Color.WHITE)); 
       } 
       setGraphic(lbl); 

      } 
     } 
    } 
+0

幫我...謝謝! – Vikram

回答

5

-fx-selection-bar-text處於根默認CSS選擇,這是Scene的選擇器所限定的彩色調色板(未CSS屬性)。我不知道你如何使用它,但如果你把它定義(全局,因爲它是場景的選擇),如:在你的CSS文件

.root{ 
    -fx-selection-bar-text: red; 
} 

然後使用-fx-selection-bar-text所有控件的CSS屬性會變成紅色。 ListView也會受到影響(請參閱下面的註釋原始用法)。
不過,如果你希望只自定義ListView的風格,覆蓋默認屬性這樣
(注:僅-fx-text-fill被重寫原始值將被註釋掉,在使用-fx-selection-bar-text):

/* When the list-cell is selected and focused */ 
.list-view:focused .list-cell:filled:focused:selected { 
    -fx-background-color: -fx-focus-color, -fx-cell-focus-inner-border, -fx-selection-bar; 
    -fx-background-insets: 0, 1, 2; 
    -fx-background: -fx-accent; 
    /* -fx-text-fill: -fx-selection-bar-text; */ 
    -fx-text-fill: red; 
} 

/* When the list-cell is selected and selected-hovered but not focused. 
    Applied when the multiple items are selected but not focused */ 
.list-view:focused .list-cell:filled:selected, .list-view:focused .list-cell:filled:selected:hover { 
    -fx-background: -fx-accent; 
    -fx-background-color: -fx-selection-bar; 
    /* -fx-text-fill: -fx-selection-bar-text; */ 
    -fx-text-fill: green; 
} 

/* When the list-cell is selected, focused and mouse hovered */ 
.list-view:focused .list-cell:filled:focused:selected:hover { 
    -fx-background: -fx-accent; 
    -fx-background-color: -fx-focus-color, -fx-cell-focus-inner-border, -fx-selection-bar; 
    -fx-background-insets: 0, 1, 2; 
    /* -fx-text-fill: -fx-selection-bar-text; */ 
    -fx-text-fill: yellow; 
} 

這些CSS屬性和更多是可用的在內置caspian.css


更新:我強烈建議你閱讀Cell API。從那裏開始

...我們使用非常少的單元格來表示極大的數據集。每個Cell都被「回收」或重複使用。

被警告的不同字符串項目可以在代碼中使用相同的細胞,具有誤導性的視覺效果/效果結束,就像​​。此外,在API它說

,因爲目前最常見的用例細胞的顯示文本到 用戶,該用例是專爲小區內的優化。這是從Labeled擴展的Cell完成的 。這意味着 單元格的子類只需設置文本屬性,而不是創建單獨的 標籤並在單元格內設置該標籤。

所以我重構你的代碼如下。

class RoomCell extends ListCell<String> { 
    @Override 
    public void updateItem(String item, boolean empty) { 
     super.updateItem(item, empty); 

     if (item != null) { 
      Log.debug("RoomCell called, item: "+item); 
      setFont(Font.font("Segoe UI", FontWeight.BOLD, 18)); 
      ImageView iView = new ImageView(); 
      if (Rooms.getBoolean(item, "OwnerStatus")) { 
       iView.setEffect(new DropShadow(15, Color.BLUEVIOLET)); 
       iView.setImage(new Image(getClass().getResourceAsStream("images/universal.png"))); 
      } else { 
       iView.setEffect(new DropShadow(15, Color.WHITE)); 
       iView.setImage(new Image(getClass().getResourceAsStream("images/yin-yang.png"))); 
      } 
      setGraphic(iView); // The image will be displayed here 
      setText(item); // The room name will be displayed here 
     } 
    } 
} 

全部-fx-text-fill單元格文本的樣式將根據CSS文件中的定義而改變。

現在,這裏是小區的文字陰影效果的影響,並從CSS文件其填充顏色之間的權衡:
- 如果你想使用陰影效果的影響,你應該去喜歡目前的方式,即創建標籤,並設置其文本,給標籤和setGraphic(標籤)帶來陰影效果。但是這次你不會喜歡設置單元格的文本(setText(item)),因此CSS文件中的文本顏色樣式將不起作用。
- 在另一方面,如果你喜歡,我已經重構,那麼你應該將其設置爲transparentnull禁用電池(延伸Labeled)的-fx-background-color並設置-fx-effect在CSS文件陰影效果是代碼能夠直接將文字陰影效果應用於文字。清除單元格的背景並不是IMO的首選方式。由代碼解釋:

Label lbl = new Label("This text will have a dropshadow on itself directly"); 
lbl.setEffect(new DropShadow(15, Color.BLUE)); 

Label another_lbl = new Label("This text will have a dropshadow applied on the background bounds, not to text"); 
another_lbl.setEffect(new DropShadow(15, Color.BLUE)); 
another_lbl.setStyle("-fx-background-color:gray"); 

測試它們以查看差異。就這樣。

+0

謝謝你Uluk。你的解決方案都可以正常工作,但在我的情況下不會。我使用CellFactory來渲染單元格,正如我在上述問題的更新中解釋的那樣。在這種情況下我能做什麼? – betaman

+0

是的,我總是打開caspian.css文件,因爲我試圖破解不同的解決方案,這是寶貴的資源。 – betaman

+1

@betaman,我在官方的ListView教程中測試了這個解決方案。 CellFactory也使用自定義單元格ColorRectCell(擴展ListCell )。發佈您的RoomCell或將其與ColorRectCell進行比較。 –

相關問題