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


Tags: css,combobox,stylesheet,javafx-8,text-styling

Problem :

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?



Solution :

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

    CSS Howto..

    How to clone the inner html of a list in query and wrap it

    How to apply CSS only if class exists elsewhere in code?

    Modify CSS class “two” if class “one” is not shown on screen

    html, svg, css how to make mouse event not fire on transparent color

    How can I add specific css id to parent menu with php or javascript

    How to center a text which is placed on an image

    How to remove a CSS style on the fly

    How to hardware accelerate a box-shadow animation in CSS?

    How to center align vertically the container in bootstrap

    How to apply custom css class to extjs grid column, and remove it with delay?

    How to use same header for multiple page inside same html file

    How to link a HTML page to a CSS file when the CSS file is in an upper directory?

    CSS Overflow, the scroll bar appears at the bottom, how can I get a scroll bar at the top?

    CSS how to solve -webkit- functions

    How to make a dotted underline link on hover?

    How to give different views for same url for different devices with same resolution?

    How to change CSS of website if accessed through a mobile browser [closed]

    How do i apply scroll for long horizontal background image?

    How To Implement 2D Perspective Within a 3D Space (CSS & jQuery)

    After adding a “current” class to an active item in a menu using jQuery, how can css changes be made to the container of the item?

    How to enqueue second stylesheet in Wordpress child theme?

    How to Set Up a Responsive Background-Image with CSS

    How to separate content and presentation layers on JSPs?

    How to place html css php in javascript (Jquery)

    I am wondering how js fiddle is handling their textareas concerning resizing and their layot

    How to use jQuery / CSS to swap out the contents of a DIV onmouseover on list items? [closed]

    How do you set a div's location to be centered when we don't know the screen size?

    How to make html list scrollable downwards on fullscreen page?

    How to align the wizard to the left and right sides?

    in html and css how to get the box space to be smaller? [closed]