JavaFX组合框项目上的文本颜色仅在首次选择后更改 [英] Text color on JavaFX ComboBox items change only after first selection

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

问题描述



我有一个简单的字符串组合框,并且在Java中使用SceneBuilder 2.0在e(fx)clipse中创建一个输入表单。想要更改列表和所选字符串中字体的颜色和大小。我使用的CSS代码更改所选项目上的文本。但是,第一次删除列表,它是黑色默认字体。第二次,所有项目的字体颜色和大小已更改为正确的值。



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



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

  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(我不知道为什么,但它是我可以让它工作的唯一方式):

  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应用程序{

@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 = null){
setText(item);
//这将不会第一次工作,但将是一个
//用于下次调用
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 = 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:16pxArial;
}
.my-list-cell {
-fx-text-fill:blue;
-fx-font:16pxArial;
/ *无替代突出显示* /
-fx-background-color:#FFF;奇怪的细节:对于JavaFX 2,你


< /stackoverflow.com/questions/25203870/combo-box-menu-item-font-change-in-fxml/25212243#25212243\">可以通过 CSS
但仍然必须使用CellFactory。


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 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?

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

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

comboBox.setItems( types );

and current css:

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

解决方案

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

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.

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

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