thatjavaguy09
thatjavaguy09

Reputation: 222

StyleManager not applying CSS properly

I'm encountering an issue with StyleManager in java 1.8.0_25. If I apply a css file to the scene directly then the css will appear properly. However, if I try and use StyleManager to apply the css file across all scenes, the css will not be applied.

Here's a simple program to demonstrate the issue

public class Test extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        Application.setUserAgentStylesheet(null);
        Parent node = new FXMLLoader(getClass().getResource("Testing.fxml")).load();
        Scene scene = new Scene(node);
//      StyleManager.getInstance().addUserAgentStylesheet("TabPaneTest.css");
        scene.getStylesheets().add("TabPaneTest.css");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

}

Testing.fxml:

<?xml version="1.0" encoding="UTF-8"?>

<?import java.net.*?>
<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>


<VBox xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <TabPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" tabClosingPolicy="UNAVAILABLE">
        <tabs>
          <Tab text="Untitled Tab 1">
            <content>
              <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
            </content>
          </Tab>
          <Tab text="Untitled Tab 2">
            <content>
              <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
            </content>
          </Tab>
        </tabs>
      </TabPane>
   </children>
</VBox>

TabPaneTest.css:

.tab-pane .tab {
    -fx-padding: 3 6 1 6;
    -fx-border-style: none;
    -fx-border-color: transparent;
    -fx-border-width: 0;
    -fx-border-insets: 2;
    -fx-background-radius: 3 3 0 0;
    -fx-background-insets: 0 2 0 2;
    -fx-background-color: linear-gradient(green 80%, blue 100%);
}

Without using StyleManager, this is what it's suppose to look like:

The css is applied when I don't use StyleManager

However, when I comment out applying the css directly to the scene and using StyleManager instead this is what I get:

Using StyleManager no css is applied

Has anyone experienced this issue with StyleManager? Unfortunately, we have a big application, that uses StyleManager to apply CSS to all scenes

Upvotes: 2

Views: 1030

Answers (2)

oshatrk
oshatrk

Reputation: 529

It doesn't get initialized properly with null value (checked on Oracle Java 8u92 32-bit). I use STYLESHEET_MODENA instead:

    ...
    @Override
    public void start(Stage primaryStage) throws Exception {
        Application.setUserAgentStylesheet(Application.STYLESHEET_MODENA);// no null!
        StyleManager.getInstance().addUserAgentStylesheet("TabPaneTest.css");
        ...

Upvotes: 2

eckig
eckig

Reputation: 11154

Call StyleManager.getInstance().addUserAgentStylesheet("TabPaneTest.css"); after primaryStage.show();.

I encountered the same issue since Java 8u40 and fixed it this way. Unfortunately I have not found a public API for adding a global stylesheet, too.

Upvotes: 2

Related Questions