CentralController_VBox2
  • root.setBackground(bg);
Code :

package javafxtemplate1;
//Central Controller Class
import java.sql.SQLException;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import java.io.IOException;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.CornerRadii;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.paint.RadialGradient;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Ellipse;
import javafx.scene.shape.Rectangle;

public class JavaFXTemplate1 extends Application {


@Override
public void start(Stage primaryStage) throws IOException, SQLException {
CentralController controller = new CentralController();
Scene scene = new Scene(controller.getRoot(), 400, 300);
primaryStage.setScene(scene);
primaryStage.setTitle("JavaFx VBox");
primaryStage.show();
}

public static void main(String[] args) {
launch(args);
}
}

//----
class CentralController {
private final VBox root;
private final Label label1, label2;
//RadialGradient gradient1;
int r1 , g1 , b1;
public CentralController() {

root = new VBox();
label1 = new Label("Central control!");
label2 = new Label("Central control!");
label1.setLayoutY(100);label1.setLayoutX(100);
Button btn = new Button("Change Color");

root.getChildren().addAll(label1,label2,btn);
root.setSpacing(8);
btn.setOnAction(new DemoEventHandler1());
}
public VBox getRoot() {
return root;
}
private class DemoEventHandler1 implements EventHandler<ActionEvent>{
@Override
public void handle(ActionEvent event) {
//Platform.exit();
Color customColor = mycolor();
String str1 = mycolor().toString();
//BackgroundFill fillGreen =
//new BackgroundFill(Color.GREEN, new CornerRadii(4), new Insets(0));
//
BackgroundFill fillrgb =
new BackgroundFill(customColor, new CornerRadii(4), new Insets(0));
//
Background bg = new Background(fillrgb);
root.setBackground(bg);
label2.setText(str1 + " red : " + r1 +" green " + g1+ " blue :" + b1);
label2.setBackground(bg);

//root.setBackground(new Background(new BackgroundFill(Color
// .rgb(17, 119, 255), CornerRadii.EMPTY, Insets.EMPTY)));
// preloaderScene.setFill(Color.rgb(17, 119, 255));
//return _color;
}
private Color mycolor(){
int red = (int) (Math.random()*256);

int green = (int) (Math.random()*256);
int blue = (int) (Math.random()*256);
Color customColor = Color.rgb(red,green,blue);
r1 = red; g1 = green;b1 = blue;
return customColor;
}
}
}

Displays: Changing VBox backgrounds, with Math random function.