[英]JavaFX custom node not rendering children
I have created a custom component which is basically a vbox with more components inside.我创建了一个自定义组件,它基本上是一个包含更多组件的 vbox。 However when I include it on the HomeView.fxml the component's children are nowhere to be seen.但是,当我将它包含在 HomeView.fxml 中时,组件的子项无处可见。 Each individual internal component is null, causing a NullPointerException on the binding part on HomeController.每个单独的内部组件都是 null,导致 HomeController 上的绑定部分出现 NullPointerException。 What am I missing?我错过了什么?
HomeView.fxml主页视图.fxml
<StackPane xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="views.home.HomeController">
<BorderPane>
<center>Placeholder</center>
</BorderPane>
<SidePane fx:id="sidePane" maxWidth="200.0" prefWidth="200.0" style="-fx-background-color: cyan;" translateX="200.0" StackPane.alignment="CENTER_RIGHT" />
</StackPane>
HomeController.java HomeController.java
public class HomeController implements Initializable {
private final HomeViewModel viewModel;
@FXML private SidePane sidePane;
public HomeController(HomeViewModel viewModel) {
this.viewModel = viewModel;
}
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
// Bind side pane - NullPointerException here, that's why it is commented out
// sidePane.imageProperty().bindBidirectional(viewModel.selectedPlatform().image());
// sidePane.nameProperty().bindBidirectional(viewModel.selectedPlatform().name());
// sidePane.identifierProperty().bind(viewModel.selectedPlatform().id().asString());
}
}
SidePane.fxml SidePane.fxml
<fx:root type="javafx.scene.layout.Region"
fx:controller="viewscomponents.sidepane.SidePane"
xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml"
prefWidth="200" maxWidth="200">
<childrenUnmodifiable>
<VBox>
<ImageView fx:id="image" />
<Label text="Name:" />
<TextField fx:id="name" />
<HBox>
<Label alignment="BOTTOM_RIGHT" text="id: " />
<Label fx:id="id" />
</HBox>
</VBox>
</childrenUnmodifiable>
</fx:root>
SidePane.java侧窗格.java
public class SidePane extends Region {
@FXML private ImageView image;
@FXML private TextField name;
@FXML private Label id;
public ObjectProperty<Image> imageProperty() {
return image.imageProperty();
}
public StringProperty nameProperty() {
return name.textProperty();
}
public StringProperty identifierProperty() {
return id.textProperty();
}
}
As James_D stated in his comment, you never load the fxml file of your custom control.正如 James_D 在他的评论中所说,您永远不会加载自定义控件的 fxml 文件。 Also I think it does not work with Region
and you should not specify a controller in the fxml file.另外我认为它不适用于Region
,您不应在 fxml 文件中指定 controller。 It should work like this:它应该像这样工作:
SidePane.java侧窗格.java
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.StringProperty;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.VBox;
import java.io.IOException;
public class SidePane extends VBox {
@FXML
private ImageView image;
@FXML
private TextField name;
@FXML
private Label id;
public SidePane() throws IOException {
FXMLLoader loader = new FXMLLoader(App.class.getResource("SidePane.fxml"));
loader.setRoot(this);
loader.setController(this);
loader.load();
}
public ObjectProperty<Image> imageProperty() {
return image.imageProperty();
}
public StringProperty nameProperty() {
return name.textProperty();
}
public StringProperty identifierProperty() {
return id.textProperty();
}
}
SidePane.fxml SidePane.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<fx:root type="VBox"
xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml"
prefWidth="200" maxWidth="200">
<ImageView fx:id="image"/>
<Label text="Name:"/>
<TextField fx:id="name"/>
<HBox>
<Label alignment="BOTTOM_RIGHT" text="id: "/>
<Label fx:id="id"/>
</HBox>
</fx:root>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.