Wish Make StyleClass writeable

warwound

Expert
Licensed User
Longtime User
I'm working on a project where i want to dynamically change the CSS classnames of 96 Labels.
A Label has the read-only property:

StyleClasses As List [read only]
Returns a List with the node's style classes.

Why is this a read-only property?

I see many java examples where a node's classnames are modified by getting the ObservableList of Strings that is returned by the node's getStyleClass() method and using standard List techniques to modify the classnames.

I'm just about to test this java library code out:

B4X:
import javafx.collections.ObservableList;
import javafx.css.Styleable;

public class Utils {

   
   public static boolean AddStyleClass(Styleable styleable1, String className){
     boolean found=false;
     ObservableList<String> existingClassNames=styleable1.getStyleClass();
     for(String existingClassName:existingClassNames){
       if(existingClassName.equals(className)){
         found=true;
         break;
       }
     }
     if(!found){
       existingClassNames.add(className);
     }
     return found;
   }
   
   public static boolean RemoveStyleClass(Styleable styleable1, String className){
     boolean foundAndRemoved=false;
     ObservableList<String> existingClassNames=styleable1.getStyleClass();
     for(String existingClassName:existingClassNames){
       if(existingClassName.equals(className)){
         existingClassNames.remove(className);
         foundAndRemoved=true;
         //   break;   //   could a classname exist in the List more than once?
       }
     }
     return foundAndRemoved;
   }
   
   public static void SetStyleClasses(Styleable styleable1, String[] classNames){
     ObservableList<String> existingClassNames=styleable1.getStyleClass();
     existingClassNames.clear();
     for(String classname:classNames){
       existingClassNames.add(classname);
     }
   }
}

If it works i'll add it as inline java to my b4j project but it'd have been handy if the StyleClass List could be made writable.
 

stevel05

Expert
Licensed User
Longtime User
The list it's self is not read only, it just means that you cannot assign a new list. But you can clear and add, remove styleclasses to the existing list.

Try
B4X:
    MainForm.RootPane.StyleClasses.Add("Test")
    Log(MainForm.RootPane.StyleClasses)
    MainForm.RootPane.StyleClasses.Clear
    Log(MainForm.RootPane.StyleClasses)
 
Top