我正在开发JavaFX应用程序的GUI,由于标识了登录用户,该GUIFX将显示/隐藏一些GUI元素。 我想通过以编程方式定义将属性写入fxml加载程序的名称空间的方式来准备此操作。这可以正常工作,但不幸的是,不能在fx:include标记中的fxml上使用。 是否可以访问所包含的fxml的加载程序的名称空间(如果是:如何?),还是应该在这里放弃使用fx:include? 任何建议都将受到高度赞赏。 预先谢谢你。
在Java中:
Properties prop = new Properties();
prop.setProperty("some_key1","some_value1");
prop.setProperty("some_key2","some_value2");
FXMLLoader loader = new FXMLLoader(getclass().getResource("/path/to/fxml.fxml"));
prop.stringPropertyNames()
.forEach(key -> loader.getNamespace().put(key,prop.getProperty(key)));
在父FXML中:
<Label fx:id="label" maxHeight="20.0" maxWidth="1.7976931348623157E308" minHeight="30.0" text="${some_key1}"/> /* here some_value1 appears */
<TabPane id="tabPane" fx:id="tabPane" minHeight="860.0" tabMaxHeight="30.0" tabMaxWidth="200.0" tabMinHeight="30.0" tabMinWidth="100.0">
<Tab id="tab" fx:id="Tab" closable="false" disable="true" text="Text">
<fx:include fx:id="tabContent" source="tab-content.fxml" />
</Tab>
</TabPane>
在子FXML中:
<Label fx:id="label" maxHeight="20.0" maxWidth="1.7976931348623157E308" minHeight="30.0" text="${some_key2}"/> /* here some_value2 does not appear */