fxml - 从属性中读取值

fxml - Reading value from properties

我是 fxml 的新手,我仍在尝试解决一些问题。通常,当我进行开发时,我会创建一些 *.properties 文件,用于从中读取值。

这是我通常做的事情:

<bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>file:${JBOSS_HOME}/standalone/configuration/someproject/properties/project.properties
                </value>
            </list>
        </property>
    </bean>

然后我在声明我的 bean 时读取任何我想要的值。像这样:

<bean id="test" class="my.package.MyClass">
    <property name="variable" value="${some.value}" />
</bean>

现在,我一直在寻找如何在 fxml 中做这样的事情,但我似乎找不到任何东西。 我很抱歉在这方面太菜鸟了,但是可以用 fxml 做这种事情吗?

比如下面的代码中,是否可以在外部定义图片的url:

<VBox alignment="CENTER" styleClass="header" stylesheets="@../styles/some.css" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <AnchorPane>
         <children>
              <ImageView AnchorPane.leftAnchor="0.0" AnchorPane.topAnchor="22.0">
                  <image>
                      <Image url="@../img/image.png" />
                  </image>
              </ImageView>
         </children>
      </AnchorPane>

PS:我正在尝试开发一个独立的应用程序,但我仍然想在外部配置一些值,而不必在需要时随时生成新版本改变一些东西。

非常感谢。

可以做到这一点,但我还没有看到很多(任何)文档。诀窍是在加载 FXML 之前访问 FXMLLoadernamespace 并使用属性填充它。

给定一个 属性 文件 application.properties:

foo=bar

你可以这样做:

Properties properties = new Properties();
Path propFile = Paths.get("application.properties"); 
properties.load(Files.newBufferedReader(propFile));

FXMLLoader loader = new FXMLLoader(getClass().getResource("path/to/fxml"));
properties.stringPropertyNames()
    .forEach(key -> loader.getNamespace().put(key, properties.getProperty(key)));
Parent root = loader.load();

然后在您的 FXML 文件中,您可以访问属性,例如

<Label text="${foo}"/>

另一种方法是在 FXMLLoader 中分配资源包。

FXMLLoader loader = new FXMLLoader();
loader.setResources(ResourceBundle.getBundle("propertyreader.application"));
Parent root = loader.load(
    getClass().getResourceAsStream(
            "about.fxml"
    )
);

访问语法略有不同(使用 % 而不是 ${)。

<Label fx:id="version" text="%version"/>

示例应用程序

示例应用假定所有文件都在名为 propertyreader.

的包中

application.properties

version=1

about.fxml

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

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.HBox?>

<HBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefWidth="400.0" spacing="10.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
    <Label text="Property Reader Version:"/>
    <Label fx:id="version" text="%version"/>
    <padding>
        <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
    </padding>
</HBox>

PropertyReaderApp.java

package propertyreader;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

import java.io.IOException;
import java.util.ResourceBundle;

public class PropertyReaderApp extends Application {
    @Override
    public void start(Stage stage) throws IOException {
        FXMLLoader loader = new FXMLLoader();
        loader.setResources(ResourceBundle.getBundle("propertyreader.application"));
        Parent root = loader.load(
            getClass().getResourceAsStream(
                    "about.fxml"
            )
        );

        stage.setScene(new Scene(root));
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}