2014-08-29 65 views
0

我遇到了一个奇怪的情况,样式为javafx中的组合框按钮。目前,我有JavaFX组合框样式按钮,如果可编辑

.combo-box .arrow { 
    -fx-background-color: black; 
} 

.combo-box .arrow-button { 
    -fx-background-color: white; 
    -fx-size: 5; 
} 

它将按钮的背景颜色设置为白色,并将箭头设置为黑色。如果组合框不可编辑,这很好。但是,如果我可以编辑组合框,则不会应用此CSS。

有没有人知道我可以如何设置组合框是可编辑的下拉按钮?

public class SSCCE extends Application{ 

@Override 
public void start(Stage primaryStage) throws Exception { 
    VBox root = new VBox(); 
    primaryStage.setScene(new Scene(root)); 

    ComboBox editable = new ComboBox(); 
    editable.setEditable(true); 
    editable.setPrefWidth(125); 

    ComboBox notEditable = new ComboBox(); 
    notEditable.setEditable(false); 
    notEditable.setPrefWidth(125); 

    root.getChildren().addAll(editable, notEditable); 
    primaryStage.sizeToScene(); 
    primaryStage.show(); 

    StyleManager.getInstance().addUserAgentStylesheet("/theme/styles/ComboBox.css"); 
} 

public static void main(String[] args) { 
    launch(args); 
} 

}

+0

为了更好地帮助越早,在后期编辑的[SSCCE(http://sscce.org)加入 – 2014-08-29 16:38:53

+0

。很明显,你将不得不改变路径到CSS文件。 – thatjavaguy09 2014-08-29 16:48:42

回答

0

StyleManager不是公共API的一部分,我不知道它做什么。

使用了标准机制,将样式表:

scene.getStylesheets().add(getClass().getResource("/theme/styles/ComboBox.css").toExternalForm()); 

enter image description here

顶部图像使用StyleManager:底部图像使用scene.getStylesheets().add(...)

+0

我了解使用Style Manager的风险,因为它不是公共API。我也使用一些太阳私人API的其他东西。尽管如此,这并不能解决问题。 – thatjavaguy09 2014-08-29 17:06:58

+0

我用你的代码(没有将CSS应用于可编辑的ComboBox的箭头)和我的代码进行了测试;使用'scene.getStylesheets()。add(...)'为我解决了这个问题。 – 2014-08-29 17:11:37

+0

有趣。使用我的应用程序,对于我为每个显示的阶段执行scene.getStylesheets()。add(...)(因为可能有很多),这将是一件麻烦事。我认为StyleManager将所有CSS样式表应用于每个场景。我的坏但我标记你是正确的。我想我将不得不进一步调查StyleManager。 – thatjavaguy09 2014-08-29 17:19:32

1

希望是不是晚来回答你的问题...为了改变箭头颜色,我们需要通过CSS组合框基类和他的子结构访问它,直到我们到达箭头:

.combo-box-base > .arrow-button > .arrow { 
    -fx-background-color: white; 
} 

你可以找到它在CSS reference for Java 8