Quite frustrating as I follow guidelines and basic tutorial. I can apply CSS styles to differnt elements but not to vbox or hbox.
I have the following simple Apps creating a simple scene using a FMXL and CSS:
import java.net.URL;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
public class BingRen extends Application {
@Override
public void start(Stage primaryStage) {
Parent root = null;
FXMLLoader loader = new FXMLLoader();
URL xmlUrl = getClass().getResource("/BingRen.fxml");
loader.setLocation(xmlUrl);
try {
root = loader.load();
Scene scene = new Scene(root,400,400);
scene.getStylesheets().add(getClass().getResource("BingRen.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
With FXML, creating just a BordPane and 2 HBox containing one label each. Almost as simple as HellopApp :
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.geometry.*?>
<BorderPane fx:id="rootBorderPane"
xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml"
fx:controller="MainControler">
<top>
<HBox>
<Label text="BingRen app" />
</HBox>
</top>
<bottom>
<HBox>
<Label text="Status bar" />
</HBox>
</bottom>
<center>
</center>
</BorderPane>
And CSS to set some basic properties:
.hbox {
-fx-background-color: #00ff00;
-fx-border-color: #00ff00;
-fx-border-width: 2px;
-fx-padding: 10;
-fx-spacing: 8;
}
.label {
-fx-text-fill: #0000ff;
}
Label properly turn blue but not of the hbox style are applied
In fact none of the suggestion worked.
I tried :
- Change .hbox to .Hbox in css file
- Make a #allbox in css file and add fx-id="allbox" and fxml file
For every change, I change the color for label to ensure the new version of css is read through.
Label always change color but I never get backgroung or paddings in the Hboxes
3
Answers
RESOLVED
Inside FXMl file:
Inside CSS file:
All all attributes are applied
The issue you are facing is because your CSS selector for the HBox elements is incorrect. You have used ".hbox" as the selector, but the correct selector is ".Hbox". The difference is the capitalization of the "b" in "HBox".
Why your current approach fails
Look at the CSS documentation.
For HBox
For Label
So there is no such style class as ".hbox" for a HBox unless you add one, which you have not done.
Background on CSS selectors and JavaFX
Read the section titled "CSS and the JavaFX Scene Graph":
Application examples
So there are three ways you can fix this:
Use a type selector in your CSS file:
Apply a style class in your CSS file:
And in FXML write:
OR in code write:
Apply a style id in your CSS file:
And in FXML write:
OR in code write:
Selector scope differences
There are differences in the meaning for the standard application of each approach: