我有一个自定义组件。
public class CustomBtn extends Button{
private ComplexObject properties;
public void setProperties(ComplexObject properties){
this.properties = properties;
}
public ComplexObject getProperties(){
return properties
}
}
public class ComplexObject {
String x = "hello";
int y = 42;
}
在FXML中,我可以通过
<Pane xmlns:fx="http://javafx.com/fxml">
<children>
<CustomBtn text="hello">
<properties>
<!-- here i want to pass the object via namespace placeholder but I don't know how -->
<properties>
</CustomBtn>
</children>
</Pane>
我想通过FXML加载器将对象传递到属性中。
public class SampleAppLauncher extends Application {
public void start(Stage primaryStage){
URL val = FXView.class.getclassLoader().getResource("sample.fxml");
FXMLLoader loader = new FXMLLoader(val);
Map<String,Object> ns = getNameSpace();
ComplexObject property = new ComplexObject();
property.x = "Bye bye";
property.y = 420;
// Here is the Object i want to pass
ns.put("props",property);
Pane p = loader.load();
Scene s = new Scene(p);
primaryStage.setScene(s);
primaryStage.show();
}
我想通过
传递参数$ {}注释
like:
<Pane xmlns:fx="http://javafx.com/fxml">
<children>
<CustomBtn text="hello">
<properties>
${props}
<properties>
</CustomBtn>
</children>
</Pane>
那可能吗?
我知道我可以使用@FXMl之类的“代码隐藏”方法,然后在加载后进行设置。但是我想了解对象是“可传递到名称空间的”对象,以及如何在FXML中配置自定义组件的“更复杂”的对象。 “命名空间映射仅用于“原始值”吗?