Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best reference for JavaFX CSS properties & selectors

Tags:

java

javafx-2

I'm trying to learn JavaFX 2, but I've been stumbling a lot trying to style my application. I've found this document which tries to document controls and the css properties that apply to them. I can't tell if it's incomplete, if I should be using some unknown selectors or JavaFX's CSS support just isn't powerful enough for my needs.

Here are a couple of examples:

  • How would I change the background color for the area behind a TabPane without coloring every other child component (is there a selector for that, or perhaps a property?)
  • How would I change the color of non-selected tabs?
like image 724
Bryan Young Avatar asked Oct 24 '11 18:10

Bryan Young


People also ask

Where do I put CSS in JavaFX?

The default css for all JavaFX applications is written in a file called modena. css , which can be found in the JavaFX runtime jar file, jfxt. jar , located in your Java installation folder. This css file defines the styles for the root node and the UI controls.

Can CSS be used in JavaFX?

CSS styles are applied to nodes in the JavaFX scene graph in a way similar to the way CSS styles are applied to elements in the HTML DOM. Styles are first applied to the parent, then to its children.

What is JavaFX CSS?

The package javafx. css contains the classes that are used to apply CSS for JavaFX applications. A CSS comprises of style rules that are interpreted by the browser and then applied to the corresponding elements in your document. A style rule is made of three parts, which are −


1 Answers

Have you tried something like this?

This uses an ID selector as shown in the "Skinning JavaFX Applications with CSS" document. You could also leave off the "#MyTabPane" selector and have it apply to all TabPane's. (It looks like the .tab and .tab-content-area selectors are not discussed in the reference guide. I went to the "caspian.css" file contained in jfxrt.jar file to find them.)

TabExample.css

#MyTabPane .tab {
    -fx-background-color: blue;
}
#MyTabPane .tab:selected {
    -fx-background-color: red;
}

#MyTabPane .tab-content-area {
    -fx-background-color: cyan;
}

#MyTabPane .tab *.tab-label {
    -fx-text-fill: white;
}

TabPaneEx.java

@Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Hello World");
        StackPane root = new StackPane();
        TabPane pane = new TabPane();
        pane.setId(("MyTabPane"));
        Tab tab1 = new Tab("ONE");
        Tab tab2 = new Tab("TWO");
        Tab tab3 = new Tab("THREE");
        pane.getTabs().addAll(tab1,tab2,tab3);
        Scene scene = new Scene(root, 300, 250);
        root.getChildren().add(pane);
        scene.getStylesheets().add(
                this.getClass().getClassLoader().getResource("tabpaneex/TabExample.css").toString());
        primaryStage.setScene(scene);
        primaryStage.show();
    }
like image 82
JimClarke Avatar answered Nov 14 '22 21:11

JimClarke