0

I want to maintain single background color(black) for all panes, and for all views. i don't want write css for every view. i am using only vbox and hbox mostly. and very few table views. is there any easy way to write css once and apply to all. thank you in advance

Gaali Prabhakar
  • 583
  • 6
  • 23

3 Answers3

1

You don't write a css for every view, you give every element the same style class.

    Pane pane = new Pane();
    pane.getStyleClass().add("bg-black-style");

Somewhere you need to add the stylesheet to the scene

scene.getStylesheets().add("css-file.css");

And in the css file

.bg-black-style {
    -fx-background-color:  black;
}

This way every thing that should look the same has it's style all in one place.

brian
  • 10,619
  • 4
  • 21
  • 79
1

You can just use .pane in CSS class, and it will work for all the panes.

    .pane{
        -fx-background-color:  black;
    }

Same works with .button etc.

phraxos
  • 200
  • 12
  • Just a heads up. As of right now, this is not working. (Atleast for me) However, it does work for other elements like buttons. – Riley Fitzpatrick Jan 29 '19 at 22:36
  • it didn't work for me either for split pane or anchorpanes, however, i was able to get what i wanted by using just an asterisk in place of .pane so it looks like this instead: * { ... } – luckyguy73 May 22 '19 at 18:10
0

You can apply the style sheet to the entire application like this:

package hacks;

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ListView;
import javafx.scene.control.TextArea;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;

import java.net.URL;

/**
 * Created by BDay on 7/10/17.<br>
 * <br>
 * CssStyle sets the style for the entire project
 */
public class CssStyle extends Application {
    private String yourCss = "YourResource.css";

    public CssStyle() {
        try {
            Application.setUserAgentStylesheet(getCss()); //null sets default style
        } catch (NullPointerException ex) {
            System.out.println(yourCss + " resource not found");
        }
    }

    private Button button = new Button("Button Text");
    private TextArea textArea = new TextArea("you text here");
    private ObservableList<String> listItems = FXCollections.observableArrayList("one", "two", "three");
    private ListView listView = new ListView<String>(listItems);
    private FlowPane root = new FlowPane(button, textArea, listView);
    private Scene scene = new Scene(root);

    @Override
    public void start(Stage primaryStage) throws Exception {
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private String getCss() throws NullPointerException {
        ClassLoader classLoader = getClass().getClassLoader();
        URL resource = classLoader.getResource(yourCss);
        String asString = resource.toExternalForm(); //throws null
        return asString;
    }
}
Bday
  • 1,005
  • 7
  • 13