gpt4 book ai didi

java - 将自定义类型的 FXML 属性设置为自定义 javafx 组件的属性

转载 作者:行者123 更新时间:2023-12-02 02:33:59 25 4
gpt4 key购买 nike

我创建了自定义 JavaFX 组件,如 http://docs.oracle.com/javafx/2/fxml_get_started/custom_control.htm 中所述。 .

// Component.fxml
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.VBox?>
<fx:root type="javafx.scene.layout.VBox" xmlns:fx="http://javafx.com/fxml">
<Label fx:id="label"/>
</fx:root>

// Component.java
public class Component extends VBox {

private final Entity entity;
@FXML private Label label;

public Component(@NamedArg("entity") Entity entity) {
this.entity = entity;
FXMLLoader fxmlLoader = new FXMLLoader();
fxmlLoader.setLocation(Component.class.getResource("/Component.fxml"));
fxmlLoader.setRoot(this);
fxmlLoader.setController(this);

try {
fxmlLoader.load();
} catch (IOException exception) {
throw new RuntimeException(exception);
}
}

@FXML
private void initialize() {
label.textProperty().set(this.entity.toString());
}
}

现在我将其导入根布局(也是自定义组件)。

// Root.fxml
<?xml version="1.0" encoding="UTF-8"?>

<?import foo.bar.javafx.Component?>
<?import javafx.scene.layout.AnchorPane?>
<fx:root type="javafx.scene.layout.AnchorPane"xmlns:fx="http://javafx.com/fxml">
// TODO How to pass entity?
<Component entity=?>
</Component>

// Root.java
public class Root extends AnchorPane {

public Root() {
FXMLLoader fxmlLoader = new FXMLLoader();
fxmlLoader.setLocation(Root.class.getResource("/Root.fxml"));
fxmlLoader.setRoot(this);
fxmlLoader.setController(this);

try {
fxmlLoader.load();
} catch (IOException exception) {
throw new RuntimeException(exception);
}
}

}

如何将实体传递给组件?我知道如何传递字符串 - 它只是 <Component entity="text"> 。但是如何传递任意类的实例呢?附:不想为此使用单例模式。

最佳答案

问题的解决方案可能是将组件与 Controller 分开,例如为每个 Controller 创建一个 Controller 类:虽然该组件是 VBox,但您可以为其定义 .fxml,如下所示:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.layout.VBox?>
<VBox xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml"
fx:controller="stackoverflow.one.ComponentController">

</VBox>

它是包含实体的 Controller :

public class ComponentController implements Initializable{

private Entity entity;

@Override
public void initialize(URL location, ResourceBundle resources) {

}

public Entity getEntity() {
return entity;
}

public void setEntity(Entity entity) {
this.entity = entity;
}
}

您还可以像这样更改 Roor:

<?import javafx.scene.layout.AnchorPane?>
<AnchorPane xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml"
fx:controller="stackoverflow.one.RootController">
<fx:include source="ComponentController.java" fx:id="component"/>

</AnchorPane>

然后在 Controller 中,您可以获得 ComponentController 的引用,并且可以设置从服务获取的实体。

public class RootController implements Initializable {

@FXML
private ComponentController componentController;

@Override
public void initialize(URL location, ResourceBundle resources) {
// getting Entity from a service or from any other sources
//You may replace this line with an appropriate one for you.
Entity entity = getEntity();
componentController.setEntity(entity);
}
}

现在您已经分离了 Controller 和 View 组件,您甚至不需要加载它们,因为 FXMLLoader 会为您完成此操作。这将是如何将对象传递到另一个 View 的 Controller 的基本概念。

关于java - 将自定义类型的 FXML 属性设置为自定义 javafx 组件的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46662852/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com