JavaFX 8 - 如何通过CSS更改NOT可编辑组合框的提示文本的颜色? [英] JavaFX 8 - How to change the color of the prompt text of a NOT editable combobox via CSS?
问题描述
标题说一切。我想改变不可编辑的组合框
的提示文本的颜色,以便文本具有与可编辑的提示文本相同的颜色, combobox
。
在我的CSS文件中,我试图在 .combo-box $ c $中使用
-fx-prompt-text-fill
c>, .combo-box-base
, .combo-box-base .text-field
和 .combo-box-base .text-input
,但没有效果。
The title says everything. I want to change the color of the prompt text of a not editable combobox
, so that the text has the same color like the prompt text of a editable combobox
.
In my CSS-file I tried to use -fx-prompt-text-fill
in .combo-box
, .combo-box-base
, .combo-box-base .text-field
and .combo-box-base .text-input
, but nothing worked.
我必须使用什么styleclass? p>
What styleclass do I have to use?
推荐答案
当 ComboBox
无法编辑时,没有 TextField
,并且属性 -fx-prompt-text-fill
不再有效,因为控件显示为 ListCell
,不扩展 TextInputControl
。
When the ComboBox
is not editable, there is no TextField
, and the property -fx-prompt-text-fill
is no longer valid, since the control displayed instead, a ListCell
, doesn't extend TextInputControl
.
这个单元格的样式,我们可以提供我们定制的样式 ListCell
:
In order to set the style of this cell, we can provide our custom styled ListCell
:
@Override
public void start(Stage primaryStage) {
ComboBox comboBox = new ComboBox();
comboBox.getItems().addAll("Item 1", "Item 2", "Item 3");
comboBox.setPromptText("Click to select");
comboBox.setEditable(false);
comboBox.setButtonCell(new ListCell(){
@Override
protected void updateItem(Object item, boolean empty) {
super.updateItem(item, empty);
if(empty || item==null){
// styled like -fx-prompt-text-fill:
setStyle("-fx-text-fill: derive(-fx-control-inner-background,-30%)");
} else {
setStyle("-fx-text-fill: -fx-text-inner-color");
setText(item.toString());
}
}
});
Scene scene = new Scene(new StackPane(comboBox), 300, 250);
primaryStage.setScene(scene);
primaryStage.show();
}
这篇关于JavaFX 8 - 如何通过CSS更改NOT可编辑组合框的提示文本的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!