JavaFX ComboBox 项上的文本颜色仅在第一次选择后才会更改 [英] Text color on JavaFX ComboBox items change only after first selection

查看:27
本文介绍了JavaFX ComboBox 项上的文本颜色仅在第一次选择后才会更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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.

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

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

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

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

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

推荐答案

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

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<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 中指定以编程方式设置,因为这对维护您的软件的任何人都非常有用.

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

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

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

这篇关于JavaFX ComboBox 项上的文本颜色仅在第一次选择后才会更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆