如何在javafx中使用fxml创建自定义弹出窗口?

乔治·菲茨帕特里克(George Fitzpatrick):

GUI示例

要创建新版本,将提示用户单击GUI左下方的加号按钮,当他们单击该按钮时,我希望在GUI中心顶部放一个窗格,以便用户输入设置并指定选项对于新版本,我已经尝试过:

 private void addRelease(Event event) throws IOException {
     Popup popup = new Popup();
    NewReleasePopupController controller = new NewReleasePopupController();
    FXMLLoader loader = new FXMLLoader(getClass().getResource("Resources/NewReleasePopup.fxml"));
    loader.setController(controller);
    loader.load();
    popup.getContent().add((Parent)loader.load());
}

但是,它似乎在加载时会引发错误,我想尽可能避免加载单独的阶段,并且希望将弹出窗口的控制器嵌套在主阶段的控制器内。如果有人可以帮助我完成此工作,或者有任何更好的方法来实现此帮助,我将尝试使用popup类,将不胜感激!

错误:

Caused by: javafx.fxml.LoadException: Controller value already specified.

/D:/DropDayAIO/out/production/DropDayAIO/DropDayAIO/Resources/newReleasePopup.fxml:6

at javafx.fxml/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2621)
at javafx.fxml/javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:922)
at javafx.fxml/javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(FXMLLoader.java:980)
at javafx.fxml/javafx.fxml.FXMLLoader$Element.processStartElement(FXMLLoader.java:227)
at javafx.fxml/javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:752)
at javafx.fxml/javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2722)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2552)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2466)
at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:2435)
at DropDayAIO.HomeSceneController.addRelease(HomeSceneController.java:151)
at DropDayAIO.HomeSceneController.addNewRelease(HomeSceneController.java:128)
... 57 more
Sai Dandem :

Based on the error you mentioned, I think you already have a controller declaration in the FXML file. like with attribute.. fx:controller

Usually while having this attribute declaration in fxml , if you try to set controller again, it throws Caused by: javafx.fxml.LoadException: Controller value already specified

And I believe this should be the case, because you can notice the error is raised at processAttribute method.

javafx.fxml/javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(FXMLLoader.java:980)

You can either remove it from the fxml or dont need to set in the code (through setController()). If you say, you want the reference of the controller, you can always get through loader.getController().

最重要的是,如果您设法解决Controller value already specified错误,我想接下来您将遇到Caused by: javafx.fxml.LoadException: Root value already specified。因为您尝试两次调用FXMLLoader的load()方法。将节点分配给变量,然后使用该变量在弹出窗口中进行设置。

我相信以下代码可以解决该问题。试一下!!

private void addRelease(Event event) throws IOException {
     Popup popup = new Popup();
    FXMLLoader loader = new FXMLLoader(getClass().getResource("Resources/newReleasePopup.fxml"));
    Parent parent = (Parent)loader.load(); // I am not sure you need a cast here
    newReleasePopupController controller = loader.getController();
    popup.getContent().add(parent);
}

建议:请使用适当的类命名约定。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

JavaFX新的自定义弹出窗口

如何扩展使用FXML的自定义JavaFX组件

是否应该在JavaFX自定义控件中使用FXML?

如何在JavaScript中使用自定义值和轴标签创建自定义柱形折线图?

JavaFX-使用fxml创建自定义对话框

如何在AndroidStudio中使用Kotlin创建自定义弹出菜单?

如何在Qt中使用自定义titleBar移动窗口

如何使用绝对布局创建自定义弹出窗口?

如何在Kotlin中使用自定义字体创建TextView的自定义类?

如何在没有警告的情况下在JavaFX中使用自定义CSS?

NextJS:如何在自定义钩子中使用窗口?

WTL如何在同一窗口中使用2个自定义绘制CListViewCtr

如何在JSP中使用自定义窗口

我如何在FXML中使用自定义窗格

如何创建自定义模式弹出窗口-以及如何在其外部单击以将其关闭

如何在Java Web App中创建自定义标签?如何在JSP中使用自定义标签?

如何在wxPython中使用自定义热点创建自定义光标?

如何在以FXML样式化的JavaFX中创建内部弹出窗口

如何在Javafx-ant,maven和INNO中使用自定义* .ISS?

如何在javaFX中使用多个fxml?

如何在MVC中使用自定义路由创建自定义URL

使用自定义弹出窗口创建JavaFX ComboBox

(JavaFX FXML)在尝试创建自定义按钮时,如何获取图像以完全填充按钮?

如何在 QMainWindow 中使用自定义最小化/关闭窗口按钮?

如何在 Extjs 中的自定义窗口顶部显示确认弹出窗口?

是否可以为 MGLPolyline 创建自定义弹出窗口?

如何在 JavaFX 中创建自定义 ProgressIndicator?

如何在 tensorflow 或 pytorch 中使用自定义权重初始化创建自定义神经网络

如何在 MapControl 中动态使用具有多个引脚的多个自定义弹出窗口