【问题标题】:Text color on JavaFX ComboBox items change only after first selectionJavaFX ComboBox 项目上的文本颜色仅在第一次选择后更改
【发布时间】:2014-08-31 03:06:23
【问题描述】:

我正在使用 e(fx)clipse 中 Windows 7 上的 SceneBuilder 2.0 从 Java 8.0 在 JavaFx 中构建输入表单。

我有一个简单的字符串组合框,并且想要更改列表和选定字符串中字体的颜色和大小。我使用的 css 代码更改了所选项目上的文本。但是,第一次删除列表时,它是黑色的默认字体。第二次,所有项目的字体颜色和大小都更改为正确的值。

如何使字体列表以正确的颜色和大小启动?

这是我的 Controller 类中初始化方法的简化代码:

ObservableList<String> types = FXCollections.observableArrayList
    ( "large", "medium", "small" );

comboBox.setItems( types );

和当前的css:

#comboBox .list-cell
 {
    -fx-font-family: arial;
    -fx-font-size: 16px;
    -fx-text-fill: #a0522d; 
 }

【问题讨论】:

  • 只是一个建议,在应用启动时在 Platform.runLater 中尝试 combo.show() 和 combo.hide()。
  • 好主意,但没有奏效。奇怪的是,我可以使用 comboBox.setStyle() 预设字体系列和大小,但预设颜色不像在 comboBox.setStyle( "-fx-text-fill: #a0522d;" ); 中那样工作。

标签: java css combobox javafx-8 scenebuilder


【解决方案1】:

你必须创建一个 CellFactory 并且不能使用 CSS(我不知道为什么,但这是我让它工作的唯一方法):

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

尽管您使用的是标准 API,但我相信您也应该在 CSS 中使用它,并在 CSS 中指定第一次必须以编程方式设置,因为这对谁来维护您的软件。

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

奇怪的细节:对于 JavaFX 2,您 could set this via CSS 但仍然必须使用 CellFactory。

【讨论】:

    猜你喜欢
    • 2013-07-10
    • 1970-01-01
    • 1970-01-01
    • 2023-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多