JavaFX: The default theme

JavaFX comes with 2 themes, Modena and Caspian.  Modena is a new theme added in version 8 and it is also the default theme.

To change the theme, we can use

setUserAgentStylesheet(STYLESHEET_CASPIAN);

Consider the following example where it will start out with the default Modena theme.  This example is a based on an earlier example on radio button.

package javafxapplication24;

import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import javafx.scene.layout.VBox;

public class JavaFXApplication24 extends Application {

    RadioButton rb1 = new RadioButton("Modena");
    RadioButton rb2 = new RadioButton("Caspian");

    ToggleGroup rbGroup = new ToggleGroup();

    @Override
    public void start(Stage stage) {

        stage.setTitle("JavaFX Program");

        VBox vbox_pane = new VBox();
        vbox_pane.setAlignment(Pos.CENTER);

        HBox hbox1 = new HBox();
        HBox hbox2 = new HBox();
        HBox hbox3 = new HBox();

        hbox1.setPadding(new Insets(15, 12, 15, 12));
        hbox1.setAlignment(Pos.CENTER_LEFT);

        hbox2.setPadding(new Insets(15, 12, 15, 12));

        rb1.setToggleGroup(rbGroup);
        rb2.setToggleGroup(rbGroup);

        hbox2.setAlignment(Pos.CENTER_LEFT);

        Button btn = new Button("Submit");

        btn.setOnAction(e -> buttonClick());

        hbox1.getChildren().add(rb1);
        hbox2.getChildren().add(rb2);
        hbox3.getChildren().add(btn);

        vbox_pane.getChildren().addAll(hbox1, hbox2, btn);

        Scene scene = new Scene(vbox_pane, 300, 250);
        stage.setScene(scene);

        stage.show();
    }

    private void buttonClick() {

        if (rb1.isSelected()) {
            System.out.println("You like Modena Theme");
            setUserAgentStylesheet(STYLESHEET_MODENA);
        }
        if (rb2.isSelected()) {
            System.out.println("You like Caspian Theme");
            setUserAgentStylesheet(STYLESHEET_CASPIAN);
        }
    }

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

This is the default Modena theme.

modena

This is the Caspian Theme.

caspian

 

 

Series Navigation<< JavaFX: Creating Menu Items
JavaFX: Applying CSS style to a scene >>