Text color on JavaFX ComboBox items change only after first selection

前端 未结 1 393
没有蜡笔的小新
没有蜡笔的小新 2020-12-21 17:46

I am building an input form in JavaFx from Java 8.0 using SceneBuilder 2.0 on Windows 7 in e(fx)clipse.

I have a simple String ComboBox, and want to change the color

1条回答
  •  有刺的猬
    2020-12-21 18:16

    You have to create a CellFactory and you can't use CSS ( I don't know why but it's the only way I could get it to work):

    import javafx.application.Application;
    import javafx.collections.FXCollections;
    import javafx.scene.Scene;
    import javafx.scene.control.ComboBox;
    import javafx.scene.control.ListCell;
    import javafx.scene.control.ListView;
    import javafx.scene.layout.Pane;
    import javafx.scene.paint.Color;
    import javafx.scene.text.Font;
    import javafx.stage.Stage;
    import javafx.util.Callback;
    
    public class Mainu extends Application {
    
        @Override
        public void start(Stage stage) throws Exception {
            ComboBox cb = new ComboBox();
            cb.setItems(FXCollections.observableArrayList("Foo","Bar","777","Batman"));
            cb.setCellFactory(new Callback, ListCell>() {
                @Override public ListCell call(ListView p) {
                    return new ListCell() {
                        @Override
                        protected void updateItem(String item, boolean empty) {
                            super.updateItem(item, empty);
                                if (item != null) {
                                    setText(item);  
                 //This won't work for the first time but will be the one
                 //used in the next calls
                                    getStyleClass().add("my-list-cell");
                                    setTextFill(Color.RED);
                                    //size in px
                                    setFont(Font.font(16));
                                }
                        }
                    };
                }
            });
            cb.getSelectionModel().selectFirst();
            Pane root = new Pane();
            root.getChildren().add(cb);
            Scene scene = new Scene(root);
            scene.getStylesheets().add(getClass().getResource("style.css").toExternalForm());
            stage.setScene(scene);
            stage.show();
        }
        public static void main(String[] args) {launch(args);}
    }
    

    Despite of the fact that you are using the standard API, I believe that you also should use it in the CSS and specify in the CSS that the first time have to be set programmatically as this will be useful for whoever will maintain your software.

    style.css

    .combo-box .cell{
        -fx-text-fill: blue;
        -fx-font: 16px "Arial";
    }
    .my-list-cell {
        -fx-text-fill: blue;
        -fx-font: 16px "Arial";
        /* No alternate highlighting */
        -fx-background-color: #FFF;
    }
    

    The curious detail: for JavaFX 2 you could set this via CSS but still had to use a CellFactory.

    0 讨论(0)
提交回复
热议问题