【发布时间】:2015-07-28 05:59:30
【问题描述】:
首先,我给你我的代码。
组件
public class Schuiver extends VBox{
private final SchuiverCompanion companion;
public Schuiver(String text) {
try {
FXMLLoader loader = new FXMLLoader(
Schuiver.class.getResource("nuleenschuiver.fxml"));
loader.setRoot(this);
this.companion = new SchuiverCompanion();
loader.setController(companion);
companion.setText(text);
loader.load();
} catch (IOException exception) {
throw new RuntimeException(exception);
}
}
}
组件控制器
public class SchuiverCompanion {
public TextField kleurveld;
public Label titel;
public Slider schuiver;
public void initialize() {
kleurveld.textProperty().bindBidirectional(schuiver.valueProperty(),
new NumberStringConverter());
}
public void setText(String text){
titel.setText(text);
}
}
COMPONENTFXML
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<fx:root fx:id="vbox" minHeight="-1.0" prefHeight="-1.0" prefWidth="-1.0" spacing="5.0" type="javafx.scene.layout.VBox" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="kleurenkiezer.SchuiverCompanion">
<children>
<Label fx:id="titel" alignment="TOP_LEFT" text="Hier een titel" />
<HBox prefHeight="44.0" prefWidth="299.0">
<children>
<Slider fx:id="schuiver" prefHeight="14.0" prefWidth="215.0" />
<TextField fx:id="kleurveld" prefHeight="25.0" prefWidth="76.0" />
</children>
</HBox>
</children>
</fx:root>
就像你看到我的组件询问一个将被放置在标签中的字符串, 问题是,如果您像这样创建对象,则无法在 fxml 文件中传递参数:
<Schuiver fx:id="red" GridPane.columnIndex="1" GridPane.rowIndex="0"/>
<Schuiver fx:id="blue" GridPane.columnIndex="1" GridPane.rowIndex="1"/>
所以我的问题是,如果我使用我的组件启动程序,如何更改标签“Titel”的文本?标题应该类似于 fix:id。
【问题讨论】:
标签: java javafx components