Andrew Bystrov Andrew Bystrov - 18 days ago 5
Java Question

Fxml set styleclass from source

I have class with final static fields :

package com.a.b;

public class MyVars {
public static final String S1 = "s1";
public static final String S2 = "s2";
....
}


And I want to set styleClass property for button from this class. Inside fxml file, I do :

<?import com.a.b.MyVars?> // import my class

<Button fx:id="myBtn">
<styleClass>
<String fx:value=MyVars.S1 /> //here error
<String fx:value=MyVars.S2 />
</styleClass>
</Button>


But I have got the error :


Message: Open quote is expected for attribute "fx:value" associated
with an element type "String".


I try to write different versions, but always got a error.

How I can write styleClass ( or another attribute) via link from source?

Answer

Use <fx:constant>:

<?import  com.a.b.MyVars?> // import my class

<Button fx:id="myBtn">
   <styleClass>
      <MyVars fx:constant="S1" />  //here error
      <MyVars fx:constant="S2" />
   </styleClass>
</Button>