Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to update stylesheets at runtime?

user13665758Aug 4 2013 — edited Aug 7 2013

Strangely the style does'nt change if i manually change the css class rules in one of the css document and switch between the 2 files.

  @Override

  public void initialize(URL url, ResourceBundle rb) {

    final String cssUrl1 = getClass().getResource("/tracker/view/fxmlgui1.css").toExternalForm();

    final String cssUrl2 = getClass().getResource("/tracker/view/fxmlgui2.css").toExternalForm();

    rootPane.getStylesheets().add(cssUrl1);

    rootPane.addEventHandler(KeyEvent.KEY_PRESSED, new EventHandler<KeyEvent>() {

      @Override

      public void handle(KeyEvent keyEvent) {

        if (keyEvent.getCode().equals(KeyCode.DIGIT1)) {

          rootPane.getStylesheets().clear();

          rootPane.getStylesheets().add(cssUrl1);

          msgLabel.setText("Css1 selected.");

        } else if (keyEvent.getCode().equals(KeyCode.DIGIT2)) {

          rootPane.getStylesheets().clear();

          rootPane.getStylesheets().add(cssUrl2);

          msgLabel.setText("Css2 selected.");

        }

      }

    });

  }

The event occurs and the style change from css1 and css2 as they was at the time of stage rendering, but if then i change one of the rules in the sylesheets after the stage is layed out, the style does'nt change anymore.

So wich is the general way to change the style of nodes changing the css document at runtime?

It's as the initial stylesheets in the css documents are someway cached and does'nt update at any switch.

This post has been answered by James_D on Aug 5 2013
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 4 2013
Added on Aug 4 2013
14 comments
12,187 views