如何将变量从控制器代码传递到fxml视图? [英] How to pass variable from controller code to fxml view?
问题描述
我有自定义组件,标签很少,textField很少。我需要实例化它3次,但每个版本必须有所有标签前缀为不同的字符串。
我的组件片段fxml:
< Label text =包含类型:/>
< Label text =inclusions size:GridPane.rowIndex =1/>
< Label text =包含数:GridPane.rowIndex =2/>
我想实现某种代码占位符,如:
< Label text =$ variable inclusions type:/>
< Label text =$ variable size:GridPane.rowIndex =1/>
< Label text =$ variable number:GridPane.rowIndex =2/>
我尽量避免逐个注入所有标签,因为我知道不可能全部注入像ex一样向控制器发出标签。 收集和LT;标签> allLabels;
问题:如何将String从控制器代码传递到fxml视图,避免重复和不必要的工作?
您可以使用
inclusions.fxml
<?xml version =1.0encoding =UTF -8\" >?;
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.VBox?>
< VBox spacing =10xmlns:fx =http://javafx.com/fxml>
< Label text =$ {name +'inclusions type'}/>
< Label text =$ {name +'inclusions size'}/>
< Label text =$ {name +'inclusions number'}/>
< / VBox>
NamespaceAware.java
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
public class NamespaceAware extends Application {
@Override
public void start(final Stage stage)抛出异常{
FXMLLoader loader = new FXMLLoader();
loader.getNamespace()。put(name,Foobar);
loader.setLocation(getClass()。getResource(inclusions.fxml));
Pane content = loader.load();
content.setPadding(new Insets(10));
stage.setScene(new Scene(content));
stage.show();
}
public static void main(String [] args){
launch(args);
}
}
I have custom component with few labels and few textFields. I need to instantiate it 3 times, but each version must have all labels prefixed with different String.
Fragment of my component fxml:
<Label text="inclusions type:"/>
<Label text="inclusions size:" GridPane.rowIndex="1"/>
<Label text="inclusions number:" GridPane.rowIndex="2"/>
I would like to achieve some kind of code placeholder like:
<Label text="$variable inclusions type:"/>
<Label text="$variable size:" GridPane.rowIndex="1"/>
<Label text="$variable number:" GridPane.rowIndex="2"/>
I try to avoid injecting all labels one by one, as I know there is no possibility to inject all labels at once to controller like ex. Collection<Label> allLabels;
Question: How to pass String from controller code to fxml view, avoiding duplication and unnecessary work?
You can use a binding expression in your FXML to grab variable values out of the FXML namespace.
In the following example, we successfully inject the name "Foobar".
inclusions.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.VBox?>
<VBox spacing="10" xmlns:fx="http://javafx.com/fxml">
<Label text="${name + ' inclusions type'}"/>
<Label text="${name + ' inclusions size'}"/>
<Label text="${name + ' inclusions number'}"/>
</VBox>
NamespaceAware.java
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
public class NamespaceAware extends Application {
@Override
public void start(final Stage stage) throws Exception {
FXMLLoader loader = new FXMLLoader();
loader.getNamespace().put("name", "Foobar");
loader.setLocation(getClass().getResource("inclusions.fxml"));
Pane content = loader.load();
content.setPadding(new Insets(10));
stage.setScene(new Scene(content));
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
这篇关于如何将变量从控制器代码传递到fxml视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!