e4-Javafx:如何将当前主题应用于登录对话框

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

我的 e4 javafx 应用程序在显示主程序之前显示登录对话框 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);
    }
    ....
}

对话框具有默认的 javafx 样式 (modena)。

如何获取当前主题并将其应用于此对话框?

您可以通过调用为组件设置默认样式集:

getStylesheets().add(new File("conf/application.css").toURI().toURL().toExternalForm());

你试过吗?

我找到了如何从当前主题中获取样式表

import org.eclipse.fx.ui.services.theme.ThemeManager;

@Inject
ThemeManager themeManager;

ObservableList<URL> stylesheets =  themeManager.getCurrentTheme().getStylesheetURL();

接下来是创建虚拟舞台并向其添加样式表;

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

然后将舞台设置为父window(第一个参数)到对话框

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

Dialog 将从父阶段复制样式表并将它们添加到自己的阶段。

有效。但我怀疑这是一种 "proper" 方式。肯定还有其他解决办法。