2014-08-29 74 views
0

我遇到了一個奇怪的情況,樣式爲javafx中的組合框按鈕。目前,我有JavaFX組合框樣式按鈕,如果可編輯

.combo-box .arrow { 
    -fx-background-color: black; 
} 

.combo-box .arrow-button { 
    -fx-background-color: white; 
    -fx-size: 5; 
} 

它將按鈕的背景顏色設置爲白色,並將箭頭設置爲黑色。如果組合框不可編輯,這很好。但是,如果我可以編輯組合框,則不會應用此CSS。

有沒有人知道我可以如何設置組合框是可編輯的下拉按鈕?

public class SSCCE extends Application{ 

@Override 
public void start(Stage primaryStage) throws Exception { 
    VBox root = new VBox(); 
    primaryStage.setScene(new Scene(root)); 

    ComboBox editable = new ComboBox(); 
    editable.setEditable(true); 
    editable.setPrefWidth(125); 

    ComboBox notEditable = new ComboBox(); 
    notEditable.setEditable(false); 
    notEditable.setPrefWidth(125); 

    root.getChildren().addAll(editable, notEditable); 
    primaryStage.sizeToScene(); 
    primaryStage.show(); 

    StyleManager.getInstance().addUserAgentStylesheet("/theme/styles/ComboBox.css"); 
} 

public static void main(String[] args) { 
    launch(args); 
} 

}

+0

爲了更好地幫助越早,在後期編輯的[SSCCE(http://sscce.org)加入 – 2014-08-29 16:38:53

+0

。很明顯,你將不得不改變路徑到CSS文件。 – thatjavaguy09 2014-08-29 16:48:42

回答

0

StyleManager不是公共API的一部分,我不知道它做什麼。

使用了標準機制,將樣式表:

scene.getStylesheets().add(getClass().getResource("/theme/styles/ComboBox.css").toExternalForm()); 

enter image description here

頂部圖像使用StyleManager:底部圖像使用scene.getStylesheets().add(...)

+0

我瞭解使用Style Manager的風險,因爲它不是公共API。我也使用一些太陽私人API的其他東西。儘管如此,這並不能解決問題。 – thatjavaguy09 2014-08-29 17:06:58

+0

我用你的代碼(沒有將CSS應用於可編輯的ComboBox的箭頭)和我的代碼進行了測試;使用'scene.getStylesheets()。add(...)'爲我解決了這個問題。 – 2014-08-29 17:11:37

+0

有趣。使用我的應用程序,對於我爲每個顯示的階段執行scene.getStylesheets()。add(...)(因爲可能有很多),這將是一件麻煩事。我認爲StyleManager將所有CSS樣式表應用於每個場景。我的壞但我標記你是正確的。我想我將不得不進一步調查StyleManager。 – thatjavaguy09 2014-08-29 17:19:32

1

希望是不是晚來回答你的問題...爲了改變箭頭顏色,我們需要通過CSS組合框基類和他的子結構訪問它,直到我們到達箭頭:

.combo-box-base > .arrow-button > .arrow { 
    -fx-background-color: white; 
} 

你可以找到它在CSS reference for Java 8