我的应用有两个阶段。第一阶段是主要阶段,它在应用启动时启动。我已经为这个阶段的FXML文件和控制器做了如下准备:
LauncherController:
public class LauncherController implements Initializable {
@FXML
private TabPane wholeTabPane;
@FXML
private Tab waitingModeTab;
@FXML
private Tab newConversationTab;
@FXML
private WaitingModeController waitingModeFXMLController;
@FXML
private NewConversationController newConversationFXMLController;
@Override
public void initialize(URL location, ResourceBundle resources) {
TabPane tabPane = wholeTabPane;
}
public void openNewConverationTab(){
wholeTabPane.getSelectionModel().select(newConversationTab);
}
}
TopMenuButtons中有一个带有openAssisstantStage()方法的按钮,可以打开一个新的小舞台:
public class TopMenuButtonsController {
public void openAssisstantStage(ActionEvent event) {
Stage stage = (Stage)((Button)event.getSource()).getScene().getWindow();
stage.setIconified(true);
FXMLLoader loadAssisstant = new FXMLLoader(this.getClass().getResource(FXMLFilePaths.ASSISSTANT_FXML));
Parent assisstant = null;
try {
javafx.geometry.Rectangle2D primaryScreenBounds = Screen.getPrimary().getVisualBounds();
assisstant = (Parent) loadAssisstant.load();
Stage waitingStage = new Stage();
waitingStage.setTitle("Asystent");
waitingStage.setScene(new Scene(assisstant));
waitingStage.setAlwaysOnTop(true);
waitingStage.initStyle(StageStyle.UNDECORATED);
waitingStage.setX(primaryScreenBounds.getMaxX() - 500);
waitingStage.setY(primaryScreenBounds.getMinY());
waitingStage.show();
} catch (IOException e) {
e.printStackTrace();
}
}
}
在这一阶段,我还有FXML文件和控制器(AssisstantController)。只有2个按钮-第一个按钮应该打开上一个舞台并关闭当前,但是第二个按钮也应该打开prevoius舞台,关闭当前并在我的TabView中打开特定的标签并执行其他一些逻辑,例如。获取实际的LocalDate:
public class AssisstantController {
@FXML
private Button maxWindowButton;
@FXML
private Button goToNewConversationButton;
@FXML
public void openMainWindow(ActionEvent actionEvent) {
Stage stage = (Stage) ((Button) actionEvent.getSource()).getScene().getWindow();
stage.close();
Launcher.getMainStage().setIconified(false);
}
@FXML
public void goToNewConversation(ActionEvent actionEvent) {
FXMLLoader loader = new FXMLLoader(getClass().getResource(FXMLFilePaths.LAUNCHER_FXML));
LauncherController controller = (LauncherController)loader.getController();
controller.openNewConverationTab();
Stage stage = (Stage) ((Button) actionEvent.getSource()).getScene().getWindow();
stage.close();
Launcher.getMainStage().setIconified(false);
//do other logic...
}
}
我不能initialize in LauncherController my AssisstantController,因为助理不是发射者的孩子。由于相同的原因,我无法使用Mediator pattern。
打开上一个阶段效果很好,但是当我尝试从FXMLLoader访问LauncherController时,我有一个NullPointerException引起,
controller.openNewConverationTab();
在AssisstantController中。我不知道它应该如何工作...我想打开previouse Stage并将特定选项卡设置为活动状态。
带有main()和start()方法的我的启动器:
public class Launcher extends Application {
public static final String APP_NAME = "...";
public static Double AppVersion = 1.1;
private static Stage mainStage;
public static Stage getMainStage() {
return mainStage;
}
public void openMainStage(Stage mainStage) {
this.mainStage = mainStage;
}
@Override
public void start(Stage primaryStage) throws Exception {
openMainStage(primaryStage);
FXMLLoader loadMainScene = FXMLLoaderSingleton.getInstance().getLoader();
loadMainScene.setLocation(this.getClass().getResource(FXMLFilePaths.LAUNCHER_FXML));
Pane mainScenePane = loadMainScene.load();
Scene mainScene = new Scene(mainScenePane);
primaryStage.setScene(mainScene);
primaryStage.setTitle(APP_NAME);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
答案 0 :(得分:0)
一种方法是 创建一个类,例如“ StageConfig”
public class StageConfig
{
private static TopMenuButtonsController tmbc = null;
private static AssisstantController ac = null;
public static void setTopMenuButtonsController(TopMenuButtonsController tmbc_val)
{
StageConfig.tmbc = tmbc_val;
}
public static TopMenuButtonsController getTopMenuButtonsController()
{
return StageConfig.tmbc;
}
public static void setAssisstantController(AssisstantController ac_val)
{
StageConfig.ac = ac_val;
}
public static getAssisstantController getController()
{
return StageConfig.ac;
}
}
现在将其初始化为示例
public class TopMenuButtonsController implements Initializable
{
@Override
public void initialize(URL location, ResourceBundle resources)
{
StageConfig.setTopMenuButtonsController(this);
}
}
现在您可以在任何要使用的地方使用控制器了。
StageConfig.getTopMenuButtonsController().SOMENODE.