JavaFX FXML 标签不更新其值
Posted
技术标签:
【中文标题】JavaFX FXML 标签不更新其值【英文标题】:JavaFX FXML Label doesn't update its value 【发布时间】:2016-05-10 10:32:02 【问题描述】:我正在尝试从另一个控制器更新标签,但它不会在视觉上更新。
以下控制器控件动态加载外部控制器。
private ClientShowController clientShowController;
public void initialize()
try
pane_clientDetail.getChildren().add(FXMLLoader.load(getClass().getResource("../resources/Client_show.fxml")));
FXMLLoader loader = new FXMLLoader(getClass().getResource("../resources/Client_show.fxml"));
loader.load();
clientShowController = (ClientShowController) loader.getController();
System.out.println(clientShowController);
catch (IOException e)
// TODO Auto-generated catch block
e.printStackTrace();
private void showPersonDetails(Person person)
if (person != null)
//Update
clientShowController.setId(Integer.toString(person.getId()));
第二个控制器:
@FXML private Label lbl_id;
public void setId(String strId)
System.out.println(strId);
lbl_id.setText(strId);
System.out.println(lbl_id);
注意:
System.out 正确显示标签上的 id; 第二个控制器与第一个控制器中嵌入的 FXML 文件相关联。如代码所示,第二个 FXML 文件/控制器是动态加载的:pane_clientDetail.getChildren().add()
。
在第二个控制器的初始化方法中设置id时,id如预期的那样显示在标签中:
公共无效初始化() this.setId("12");
编辑:
第一个(主要)FXML 定义:
<Pane fx:id="pane_clientDetail"/>
这个窗格被声明为属性
@FXML private Pane pane_clientDetail;
第二个:
<GridPane xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.controllers.ClientShowController">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" maxWidth="144.0" minWidth="10.0" prefWidth="102.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="195.0" minWidth="10.0" prefWidth="195.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Label text="given name" GridPane.rowIndex="1">
<GridPane.margin>
<Insets left="10.0" />
</GridPane.margin>
</Label>
<Label text="family name" GridPane.rowIndex="2">
<GridPane.margin>
<Insets left="10.0" />
</GridPane.margin>
</Label>
<Label text="gender" GridPane.rowIndex="3">
<GridPane.margin>
<Insets left="10.0" />
</GridPane.margin>
</Label>
<Label text="email" GridPane.rowIndex="4">
<GridPane.margin>
<Insets left="10.0" />
</GridPane.margin>
</Label>
<Label text="phone" GridPane.rowIndex="5">
<GridPane.margin>
<Insets left="10.0" />
</GridPane.margin>
</Label>
<Label text="id">
<GridPane.margin>
<Insets left="10.0" />
</GridPane.margin>
</Label>
<Label text="address" GridPane.rowIndex="6">
<GridPane.margin>
<Insets left="10.0" />
</GridPane.margin>
</Label>
<Label text="zip" GridPane.rowIndex="7">
<GridPane.margin>
<Insets left="10.0" />
</GridPane.margin>
</Label>
<Label fx:id="lbl_id" text="-" GridPane.columnIndex="1">
<GridPane.margin>
<Insets />
</GridPane.margin>
</Label>
<Label fx:id="lbl_given_name" text="-" GridPane.columnIndex="1" GridPane.rowIndex="1" />
<Label fx:id="lbl_family_name" text="-" GridPane.columnIndex="1" GridPane.rowIndex="2" />
<Label fx:id="lbl_gender" text="-" GridPane.columnIndex="1" GridPane.rowIndex="3" />
<Label fx:id="lbl_email" text="-" GridPane.columnIndex="1" GridPane.rowIndex="4" />
<Label fx:id="lbl_phone" text="-" GridPane.columnIndex="1" GridPane.rowIndex="5" />
<Label fx:id="lbl_address" text="-" GridPane.columnIndex="1" GridPane.rowIndex="6" />
<Label fx:id="lbl_zip" text="-" GridPane.columnIndex="1" GridPane.rowIndex="7" />
<Label text="city" GridPane.rowIndex="8">
<GridPane.margin>
<Insets left="10.0" />
</GridPane.margin>
</Label>
<Label fx:id="lbl_city" text="-" GridPane.columnIndex="1" GridPane.rowIndex="8" />
</children>
<VBox.margin>
<Insets top="10.0" />
</VBox.margin>
</GridPane>
【问题讨论】:
你也可以添加fxml的定义吗?嵌入式是什么意思? 【参考方案1】:您似乎有两个 UI 实例:
1. pane_clientDetail.getChildren().add(FXMLLoader.load(getClass().getResource("../resources/Client_show.fxml")));
2. FXMLLoader loader = new FXMLLoader(getClass().getResource("../resources/Client_show.fxml"));
您首先加载一个视图并将其附加到pane_clientDetail
,这就是正在显示的视图。然后你创建另一个并获得它的控制器。由于显而易见的原因,第二个控制器不会更新第一个视图。
解决方案:
FXMLLoader loader = new FXMLLoader(getClass().getResource("../resources/Client_show.fxml"));
Parent view = loader.load();
pane_clientDetail.getChildren().add(view);
// rest of the code
【讨论】:
以上是关于JavaFX FXML 标签不更新其值的主要内容,如果未能解决你的问题,请参考以下文章
动态访问/遍历/操作从控制器类外部的 FXML 创建的 JavaFX 节点