This discussion is archived
6 Replies Latest reply: Jan 28, 2013 5:46 PM by bvm RSS

setStyle vs getStyleClass().add updates

mfortner Newbie
Currently Being Moderated
I was wondering why there seems to be a difference between when the effects of using setStyle and getStyle().add() are made apparent on the screen? When I use setstyle to change the way a node is rendered in a chart, the effect is immediate. If I set the style class, then the change is not apparent until sometime later. Is there any way of forcing the style class change to be seen immediately?
  • 1. Re: setStyle vs getStyleClass().add updates
    David Grieve Pro
    Currently Being Moderated
    getStyle().add() ??? Do you mean getStyleClass().add() ?

    There shouldn't be any difference. The node should be updated on the next pulse. Have you run a profiler?
  • 2. Re: setStyle vs getStylesheets().add updates
    mfortner Newbie
    Currently Being Moderated
    Haven't run a profile, just noticed the difference. I have a node decorator that changes the fill colors of chart symbols in a line chart. If I set the symbol's fill color with setStyle, the results are immediately apparent. If I set the fill color with getStylesheets().add() and getStylesheets().remove(), the results aren't immediately apparent.
  • 3. Re: setStyle vs getStylesheets().add updates
    Narayan Pro
    Currently Being Moderated
    Hi mfortner,


    Lets make clear about the JavaFX CSS and function.
    If you are dressing up with Node then you can do it either using INLINE CSS or different STYLESHEETS or different STYLECLASS or different ID.

    To style your node from.
    1. By inline-id: setStyle()  //(This changes only node)
    2. By class: getStyleClass().add()    //(This changes  only specific group of class name)
    3. By different stylesheets (.css) : parent.getStyleSheets().add()  //(This affects whole style and may change effect on all the nodes)
    4. By id: setId()// (This changes effect on specific element/node. And it is very unique)
    In your case I think you need to use the getStyleClass().add() and getStyleClass().remove() for making changes on your node instantly.



    Thanks
    Narayan
  • 4. Re: setStyle vs getStylesheets().add updates
    David Grieve Pro
    Currently Being Moderated
    If you are using add/remove on getStylesheets(), then you're causing the stylesheet to be re-parsed. This could explain the delay you are seeing. As Narayan points out, it would be better to use multiple style classes.
    .rect.blue { -fx-fill: blue; }
    .rect.red { -fx-fill: red; }
    .rect.red.blue { -fx-fill: purple; }
    
    Rectangle rect = new Rectangle(50,50);
    rect.getStyleClass().addAll("rect", "red");
    
    rect.getStyleClass().remove("red");
  • 5. Re: setStyle vs getStylesheets().add updates
    mfortner Newbie
    Currently Being Moderated
    Sorry, I fat-fingered that last response. I should have said that I set the class (not the style sheet). In my case I have a stylesheet similar to the one you showed below. The styles in the stylesheet change the background color of a chart symbol based on the data value of the node. For the sake of argument lets call those css classes "highlighted", "normal".

    If I do this:
        for (Data<X,Y> dataItem : mySeries){
             Node node = dataItem.getNode();
             boolean isHighlighted = (Boolean)node.getUserData();
             if (isHighlighted){
                  node.getStyleclass().remove("normal");
                  node.getStyleclass().add("highlight");  
             } else {
                 ....
             }
        }
    then I don't see the change immediately.

    If I simply set the style definition for the node (i.e. node.setStyle("-fx-fill: black"), then the change is immediately apparent.
  • 6. Re: setStyle vs getStyleClass().add updates
    bvm Newbie
    Currently Being Moderated
    There's a known bug (http://javafx-jira.kenai.com/browse/RT-25332) where removing from getStyleClass() does not update the node. Might that be what you're seeing?

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points