5 Replies Latest reply: Jan 15, 2013 7:23 AM by YacineBelkessam RSS

    JavaFx Stylesheet Dynamically Change

    YacineBelkessam
      Hi,

      I would like to set up stylesheet from string rather that from url.

      I would like to have something like:

      Control c = new ()...
      c.setStyleSheet(".titled-pane {     -fx-text-fill: red; -fx-collapsible: false; }");

      I need to dynamically change the stylesheet..

      is this possible?

      Many thanks for you help.
        • 1. Re: JavaFx Stylesheet Dynamically Change
          James_D
          You can't provide a string in place of an external style sheet.

          You can set the style on an individual node using setStyle(...). That will only affect the node on which you call the method, and you can't use selectors inside it.

          The closest equivalent to what you are trying to do would be something like
          Node titledPane = c.lookup(".titled-pane");
          titledPane.setStyle(" -fx-text-fill: red; -fx-collapsible: false; ");
          A better solution may be to define the possible styles in an external style sheet, using different (logical) class names, and to dynamically change the style class of the control. (This relies on knowing all possible styles you may want to use, which of course may not be the situation you're in.)
          • 2. Re: JavaFx Stylesheet Dynamically Change
            YacineBelkessam
            Hi,

            Many thanks for your reply.

            My problem with the solution you suggested me is that:

            1 - c.lookup(".titled-pane") return a null pointer. I read that the Node must be shown before lookup may return the child node...

            2 - I will need to specify the style for every child node, which can be a bit complicated depending on the node...

            I am developing an application where some "super" users must be able to customize the interface, and since I am building a jar file running as applet, I cannot modify stylesheet files before applying them. As a result, I have to rebuild the jar to change the css files... I do not sea the advantage of CSS if I cannot update if dynamically without rebuilding the application.

            Thanks for your help.
            • 3. Re: JavaFx Stylesheet Dynamically Change
              James_D
              Assuming your applet is signed, you could write the css to a file on the filesystem. Then, on a Scene or Parent (or subclass) you can call getStylesheets().add(...) and pass in a URL mapping to the file ("file://path.to.css").
              • 4. Re: JavaFx Stylesheet Dynamically Change
                YacineBelkessam
                Ah yes, I tested this but I did provide a file path without file://

                I will try this and poste the result.

                Many thanks for your help.
                • 5. Re: JavaFx Stylesheet Dynamically Change
                  YacineBelkessam
                  Hi James_D,

                  the solution you suggested me works perfectly.

                  Thanks a lot.