JavaFX 8 - How to change the color of the prompt text of a NOT editable combobox via CSS?

怎甘沉沦 提交于 2019-12-02 07:48:13

问题


The title says everything. I want to change the color of the prompt text of a not editable combobox, so that the text has the same color like the prompt text of a editable combobox. In my CSS-file I tried to use -fx-prompt-text-fill in .combo-box, .combo-box-base, .combo-box-base .text-field and .combo-box-base .text-input, but nothing worked.

What styleclass do I have to use?


回答1:


When the ComboBox is not editable, there is no TextField, and the property -fx-prompt-text-fill is no longer valid, since the control displayed instead, a ListCell, doesn't extend TextInputControl.

In order to set the style of this cell, we can provide our custom styled ListCell:

@Override
public void start(Stage primaryStage) {
    ComboBox comboBox = new ComboBox();
    comboBox.getItems().addAll("Item 1", "Item 2", "Item 3");
    comboBox.setPromptText("Click to select");
    comboBox.setEditable(false);

    comboBox.setButtonCell(new ListCell(){

        @Override
        protected void updateItem(Object item, boolean empty) {
            super.updateItem(item, empty); 
            if(empty || item==null){
                // styled like -fx-prompt-text-fill:
                setStyle("-fx-text-fill: derive(-fx-control-inner-background,-30%)");
            } else {
                setStyle("-fx-text-fill: -fx-text-inner-color");
                setText(item.toString());
            }
        }

    });

    Scene scene = new Scene(new StackPane(comboBox), 300, 250);
    primaryStage.setScene(scene);
    primaryStage.show();
}



回答2:


Hi I do beleive that I m providing a better solution

First

in your CSS file create the following

.input .text-field {
         -fx-prompt-text-fill: #a0a0a0; // or any color you want
}

than in the scene builder set your combobox class to input after attaching the CSS file

That works like a sharm for me



来源:https://stackoverflow.com/questions/27420047/javafx-8-how-to-change-the-color-of-the-prompt-text-of-a-not-editable-combobox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!