Search code examples
javaeclipsejavafxe4efxclipse

e4-Javafx: How to apply current theme to login dialog


My e4 javafx application displays login dialog before diplaying main window.

LifeCycleManager class

@PostContextCreate
void postContextCreate(Application app, IEclipseContext econtext){  

    //display login dialog extends org.eclipse.fx.ui.dialogs.TitleAreaDialog
    LoginDialog dialog = new LoginDialog(null, "Login", "Login title", "Message", "");
    int response = dialog.open();             

    if (response != Dialog.OK_BUTTON) {
         System.exit(0);
    }
    ....
}

Dialog has default javafx styling (modena).

How do i get current theme and apply to this dialog?


Solution

  • I found out how to get stylesheets from current theme

    import org.eclipse.fx.ui.services.theme.ThemeManager;
    
    @Inject
    ThemeManager themeManager;
    
    ObservableList<URL> stylesheets =  themeManager.getCurrentTheme().getStylesheetURL();
    

    Next is to create dummy stage and add stylesheets to it;

    Scene scene = new Scene(new HBox());
    Stage stage = new Stage;
    stage.setScene(scene);
    for (URL url : stylesheets) 
    {
        stage.getScene().getStylesheets().add(url.toExternalForm());
    }
    

    Then set stage as parent window (first parameter) to dialog

    LoginDialog dialog = new LoginDialog(stage, "Login", "Login title",
                      "Message", "");
    

    Dialog will copy stylesheets from parent stage and will add them to its own stage.

    It works. But i doubt it is a "proper" way. There must be other solutions.