A simple example - TestFX_ND_C

Display:

Java code:

import javafx.application.Application; // FX base, requires start(Stage)
import javafx.stage.Stage;             // required by start (Stage)
import javafx.scene.Scene;             // no scene --> no display

import javafx.scene.layout.GridPane;   // a layout

import javafx.scene.control.Button;    // Something to add to get stage to show
import javafx.scene.control.Label;     // Another thing to show gridpane working

public class TestFX_ND_C extends Application {

  public void start (Stage stage) {
    GridPane pane = new GridPane ();
    Scene scene = new Scene (pane);
   
    // load a custom style sheet
    scene.getStylesheets().add("TestFX_ND_C.css");
    // specification for GridPane from style sheet
    pane.setId ("pane");

    // stage won't even display unless something is added to the pane!
   
    // a button formatted using the my-button specification from the css file
    Button b01 = new Button ("One");
    // .setStyle (String) won't work here, so: getStyleClass().add (String)
    b01.getStyleClass().add ("my-button");
    // for GridPane, add using (Node, int, int) vs getChildren().add(Node)
    pane.add (b01, 1, 0);
   
    // adding a label
    pane.add (new Label ("This is a label"), 2, 0);
   
    // adding another button - in-line css formatting
    Button b02 = new Button ("Another button!");
    b02.setStyle ("-fx-background-color: blue; -fx-text-fill: white");
    pane.add (b02, 3, 0);

    stage.setScene (scene);
    stage.show ();
  } // end method start - from abstract method
 
  public static void main (String args []) {
    launch (args);
  } // end main
} // end class TestFX_ND_C

CSS File - TestFX_ND_C.css

/* File: TestFX_ND_C.css
*/

#pane {
  -fx-padding: 0.5em;
  -fx-hgap:    1.0em;
  -fx-vgap:    2.0em;
/*  -fx-background-image: url("coffee.gif"); */
  -fx-background: cyan;
} /* end pane */

.my-button {
  -fx-background-color: red;
} /* end my-button */