JavaFX style by classname

2019-02-27 17:49发布

Perhaps a little basic question, but is it possible to style elements of a table by classname in JavaFX. So for example like this:

MyClassname .table-view .column-header .label {
    -fx-text-fill:#F00;
}

I want this to style multiple tables within 1 stylesheet.

thanks in advance

2条回答
你好瞎i
2楼-- · 2019-02-27 18:05

Specify the full path to your class from root.

.root MyClassname .table-view .column-header .label {
    -fx-text-fill:#F00;
}
查看更多
太酷不给撩
3楼-- · 2019-02-27 18:11

Yes, styling based upon class name selectors is supported - it's based on Node.getTypeSelector(), which comes from the Styleable interface.

The type of this Styleable that is to be used in selector matching. This is analogous to an "element" in HTML. (CSS Type Selector).

Returns: getClass().getName() without the package name

So you could style all Labels to have a green text fill using the CSS rule:

Label { -fx-text-fill: forestgreen; }

Most nodes also have style classes which are set on them by the application or, if it is a standard control, by the JavaFX framework. The standard way most people code is not to use the type selectors but to use style class selectors instead:

.label { -fx-text-fill: forestgreen; }

The type selector information is documented in the JavaFX CSS reference guide:

Node's getTypeSelector method returns a String which is analogous to a CSS Type Selector. By default, this method returns the simple name of the class. Note that the simple name of an inner class or of an anonymous class may not be usable as a type selector. In such a case, this method should be overridden to return a meaningful value.

查看更多
登录 后发表回答