A simple example - TestFX_ND_D

Display:

Java code:

// File: TestFX_ND_D.java
// Date: Aug 30, 2014
// Author: Nicholas Duchon
// Purpose: show various ways to set the color of a Button

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

import javafx.scene.paint.Color;

public class TestFX_ND_D 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
    MyButtonD b01 = new MyButtonD ("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
    MyButtonD b02 = new MyButtonD ("Another button!");
    b02.setStyle ("-fx-background-color: blue; -fx-text-fill: white");
   
pane.add (b02, 3, 0);

    // adding another button - in-line css formatting with MyButtonD class
    MyButtonD b03 = new MyButtonD ("Another button A!");
    b03.setColor ("mediumorchid");
   
pane.add (b03, 1, 1);

    // adding another button - in-line css formatting
    MyButtonD b04 = new MyButtonD ("Another button B!", "magenta");
   
pane.add (b04, 2, 1);

    // adding another button - in-line css formatting
    MyButtonD b05 = new MyButtonD ("Another button! C", Color.LIME);
   
pane.add (b05, 1, 2);

    // adding another button - in-line css formatting
    MyButtonD b06 = new MyButtonD ("Another button D!");
    b06.setColorInvertText (Color.MEDIUMORCHID);
   
pane.add (b06, 2, 2);

    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_D

class MyButtonD extends Button {
  MyButtonD (String s) {super (s);
}  

  MyButtonD () {super ();
}  

  MyButtonD (String s, String c) {
    super (s);
    setColor (c);
 
} // end String/String=color constructor
 
  MyButtonD (String s, Color c) {
    super (s);
    setColor (c);
 
} // end String/String=color constructor
 
  void setColor (String c) {
    setStyle ("-fx-background-color: " + c);
 
} // end method setColor

  void setColor (Color c) {
    setStyle ("-fx-background-color: " + c.toString().replace("0x", "#"));
 
} // end method setColor

  void setColorInvertText (Color c) {
    setStyle (
       "-fx-background-color: " + c.toString().replace("0x", "#") +
      ";-fx-text-fill: " + c.invert().toString().replace("0x", "#")
    );
 
} // end method setColor
} // end class MyButtonD

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 */