【问题标题】:fxml - Reading value from propertiesfxml - 从属性中读取值
【发布时间】:2015-09-14 17:28:11
【问题描述】:

我是 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:我正在尝试开发一个独立的应用程序,但我仍然想在外部配置一些值,而无需在需要更改某些内容时生成新版本。

非常感谢。

【问题讨论】:

  • 您是在 JavaFX 应用程序中使用 Spring,还是只是一个比较示例?
  • @James_D 这只是一个比较的例子......

标签: java javafx properties fxml


【解决方案1】:

另一种方法是在 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);
    }
}

【讨论】:

  • 非常感谢您的回答。有了您的回答和 James_D 的回答,我觉得我可以很好地开发应用程序。再次感谢您:-)
【解决方案2】:

可以这样做,但我没有看到太多(任何)文档。诀窍是在加载 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}"/>

【讨论】:

  • 非常感谢,这正是我想要的。谢谢:-)
  • 为什么不简单地说 ResourceBundle bundle = ResourceBundle.getBundle("strings"); /* 字符串在一个名为 strings.properties 的文件中*/ Parent root = FXMLLoader.load(getClass().getResource("login.fxml"), bundle);然后像下面的答案一样使用 % 访问键名?
猜你喜欢
  • 1970-01-01
  • 2020-04-13
  • 2015-11-10
  • 1970-01-01
  • 1970-01-01
  • 2016-12-12
  • 2012-03-04
  • 2023-03-13
  • 2020-01-07
相关资源
最近更新 更多