JavaFX ComboBox 项上的文本颜色仅在第一次选择后才会更改

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24664701/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 13:55:52  来源:igfitidea点击:

Text color on JavaFX ComboBox items change only after first selection

javacsscomboboxjavafx-8scenebuilder

提问by MantaMan

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

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

I have a simple String ComboBox, and want to change the color and size of the fonts in both the list and the selected String. The css code I use changes the text on the selected item. However, the first time one drops the list, it is in black default font. The second time, the font color and size on all items have changed to the correct values.

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

How do I make the font list start up in the right color and size?

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

Here is simplified code from the initialize method in my Controller class:

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

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

comboBox.setItems( types );

and current css:

和当前的CSS:

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

回答by Mansueli

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):

你必须创建一个 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);}
}

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

尽管您使用的是标准 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;
}

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

奇怪的细节:对于 JavaFX 2,您可以通过设置它,CSS但仍然必须使用 CellFactory。