CSS中的StyleableProperty

CSS中的StyleableProperty

本文介绍了JavaFX CSS中的StyleableProperty的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用自定义控件,并且正在使用StyleablePropertyFactory尝试制作如下可设置样式的属性:

I'm working on a custom control and am using the StyleablePropertyFactory to try and make a styleable property as follows:

private static final StyleablePropertyFactory<RackControl> FACTORY = new StyleablePropertyFactory<>(RackControl.getClassCssMetaData());
    private StyleableProperty<Color> defaultTubeColor =
            FACTORY.createStyleableColorProperty(this, "defaultTubeColor", "-fx-default-tube-color", rc -> rc.defaultTubeColor);

然后,我有了获取器和设置器,可以让我访问读入的值:

I've then got getters and setters which allow me to access the read in value:

public Color getDefaultTubeColor() {
    return defaultTubeColor.getValue();
}

public void setDefaultTubeColor(Color defaultTubeColor) {
    System.out.println("Set default tube color");
    this.defaultTubeColor.setValue(defaultTubeColor);
}

@SuppressWarnings("unchecked")
public ObservableValue<Color> defaultTubeColorProperty() {

    return (ObservableValue<Color>) defaultTubeColor;
}

在构造函数中,我设置外观和样式以寻找:

In the constructor I am setting a skin and style to look out for:

    this.setSkin(new RackControlSkin(this));
    getStyleClass().add("rack-control");

然后在我的皮肤中(尽管这并不重要,我从哪里得到它)我试图按如下方式在css属性中加载:

Then in my skin (though that doesn't really matter where I'm getting it from) I am trying to load in the css property as follows:

    System.out.println(control.getCssMetaData().stream().filter(c -> c.getProperty().equals("-fx-default-tube-color")).findFirst().get());
    System.out.println(control.getDefaultTubeColor());

打印输出:

CSSProperty {property: -fx-default-tube-color, converter: ColorConverter, initalValue: 0x000000ff, inherits: false, subProperties: []}
0x000000ff

所以我知道可以从以下CSS文件中找到该属性:

So I know the property is being found from the following css file:

.rack-control {
   -fx-default-tube-color: red;
}

如我的测试代码所示,当从场景中调用它时:

When it is being called from the scene as shown in my test code:

VBox root = new VBox();
RackControl control = new RackControl(8,12);
root.getChildren().addAll(control);
VBox.setVgrow(control, Priority.ALWAYS);
Scene scene = new Scene(root, 320, 200);
String css = this.getClass().getResource("rackcontrol.css").toExternalForm();
scene.getStylesheets().add(css);
//Scene scene = new Scene((Parent) FXMLLoader.load(getClass().getResource("TestUI.fxml")));
primaryStage.setTitle("Custom control");
primaryStage.setScene(scene);
primaryStage.show();

据我所知;我做的一切都正确,但该值只是没有从css文件中加载.任何人都可以向我提供有关我在这里做错了什么的指针吗?

So as far as I can see; I'm doing everything correctly but the value just isn't being loaded in from the css file. Can anyone provide me with a pointer as to what I'm doing wrong here please?

在此先感谢您阅读问题,并感谢您的任何建议!

Thanks, in advance, for reading the question and double thanks for any advice!

干杯

尼尔

推荐答案

我们通过解构元类找到了答案-直到调用 后,才设置css属性.因此,您需要设置外观以侦听css中的更改,然后进行绘制等.

We found out the answer by deconstructing the meta classes - css properties are not set until after the constrcutor has been called. Therefore you need to set the skin up to listen to changes in the css and then do the drawing etc.

这篇关于JavaFX CSS中的StyleableProperty的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 17:31