How to add class or pseudoclass programmatically to custom control in JavaFX?

前端 未结 1 722
不知归路
不知归路 2021-01-20 19:11

In JavaScript world it is often set element class to denote it\'s appearance, which is later defined by CSS.

Is this so in JavaFX?

For example, what if I wa

1条回答
  •  迷失自我
    2021-01-20 19:42

    If you want to add a style to a Node that you can toggle on and off, a PseudoClass is indeed the correct way to do it. It was indeed added in JavaFX 8.0, but that is the current stable version, so it is a mature API. Note that this creates a pseudoclass (:classname in CSS), not a "normal" class (.classname in CSS).

    If you have a Node you want to style (lets call it node), you can use PseudoClass like this:

    node.pseudoClassStateChanged(PseudoClass.getPseudoClass("negative"), true);
    

    Do the same thing, except with false as the second argument, to turn it off again.

    0 讨论(0)
提交回复
热议问题