Combo box menu item font change in fxml

删除回忆录丶 提交于 2019-12-11 03:16:38

问题


I need to change the font and remove the shadow for the combo box items.

I've made an attempt with the code below but it was unsuccessful:

.combo-box .popup-menu, .combo-box .menu-item, .combo-box .popup-menu .menu-item-radio
    {
        -fx-shadow-highlight-color: transparent;
         -fx-font-family: "Arial";
         -fx-font-size: 14px;
    }

回答1:


For JavaFX2 you can create a CellFactory to set it:

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.stage.Stage;
import javafx.util.Callback;

public class Main extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        ComboBox<String> cb = new ComboBox<String>();
        cb.setItems(FXCollections.observableArrayList("Foo","Bar","777","Batman"));
        cb.setCellFactory(new Callback<ListView<String>, ListCell<String>>() {
            @Override public ListCell<String> call(ListView<String> p) {
                return new ListCell<String>() {
                    {
                        getStyleClass().add("customcell");
                    }
                    @Override
                    protected void updateItem(String item, boolean empty) {
                        super.updateItem(item, empty);
                        setText(item);
                    }
                };
            }
        });
        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);}
}

style.css

/* for the main button of the ComboBox  */
.combo-box .cell{
    -fx-text-fill: blue;
    -fx-font: 16px "Arial";
}
.customcell {
    -fx-text-fill: blue;
    -fx-font: 16px "Arial";
    /* No alternate highlighting */
    -fx-background-color: #FFF;
}

For an JavaFX 8 example check this answer that I gave to a very similar question but for other version.



来源:https://stackoverflow.com/questions/25203870/combo-box-menu-item-font-change-in-fxml

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