Peter Penzov
Peter Penzov

Reputation: 1588

Set default skin in JavaFX with ComboBox

I would like to change the default skin of JavaFXapplication during runtime. How I can do this using ComboBox? Now I use this code to change the value:

setUserAgentStylesheet(STYLESHEET_MODENA);

Is there a way to change the skin in a run time?

Upvotes: 0

Views: 622

Answers (1)

bluevoxel
bluevoxel

Reputation: 5358

You can use setUserAgentStylesheet() in ComboBox.setOnAction() method body. For example:

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class SetStyleDemo extends Application {

    public Parent createContent() {

        /* layout */
        BorderPane layout = new BorderPane();

        /* layout -> combobox */
        ObservableList<String> styles = FXCollections.observableArrayList(
                "Modena", "Caspian");

        ComboBox<String> cbStyles = new ComboBox<String>(styles);
        cbStyles.getSelectionModel().select(0);

        cbStyles.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent ae) {
                if (cbStyles.getSelectionModel()
                        .getSelectedItem().equals("Modena")) {

                    setUserAgentStylesheet(STYLESHEET_MODENA);
                } else {
                    setUserAgentStylesheet(STYLESHEET_CASPIAN);
                }
            }
        });

        /* add items to the layout */
        layout.setCenter(cbStyles);
        return layout;
    }

    @Override
    public void start(Stage stage) throws Exception {
        stage.setScene(new Scene(createContent()));
        stage.setWidth(200);
        stage.setHeight(200);
        stage.show();
    }

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

Upvotes: 3

Related Questions