Java FXML从绝对路径加载(动态)

时间:2014-09-01 12:06:16

标签: java fxml

我想从绝对路径或jar系统之外的路径加载fxml文件。

背景: 它将是一个简单的插件系统,看起来在所有fxml文件(后来的jar文件)的插件文件夹中,并自动包含在TabPane中。

String fxmlpath = "C:\\plugin\\pluginfxml.fxml";
try {
    Parent root = FXMLLoader.load(getClass().getResource(fxmlpath));
    //Load root in Tabpane and so on ...
}

enter image description here

2 个答案:

答案 0 :(得分:5)

应该很简单:

Parent root = FXMLLoader.load(Paths.get(fxmlpath).toUri().toURL());

FXMLLoader接受一个URL作为其参数,因此我们只使用NIO Paths 类来获取Path,然后将其转换为URL。如果程序没有对文件位置的读取权限,则会出现唯一的问题。'

我使用JavaFX教程中的示例代码充实了示例:

测试应用程序:

package javafx;

import java.net.URL;
import java.nio.file.Paths;

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

public class FXMLTest extends Application {

    public static void main(String[] args) {
        Application.launch(FXMLTest.class, args);
    }

    @Override
    public void start(Stage stage) throws Exception {
        URL fxmlURL = Paths.get("C:\\test\\fxml_example.fxml").toUri().toURL();
        Parent root = FXMLLoader.load(fxmlURL);

        stage.setTitle("FXML Welcome");
        Scene myScene = new Scene(root, 300, 275);
        stage.setScene(myScene);
        stage.show();
    }
}

示例控制器:

package javafx;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.text.Text;

public class FXMLExampleController {
    @FXML private Text actiontarget;

    @FXML protected void handleSubmitButtonAction(ActionEvent event) {
        actiontarget.setText("Sign in button pressed");
    }
}

示例FXML:

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

<?import java.net.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>


<GridPane fx:controller="javafx.FXMLExampleController" 
    xmlns:fx="http://javafx.com/fxml" alignment="center" hgap="10" vgap="10"
    styleClass="root">
    <padding><Insets top="25" right="25" bottom="25" left="25"/></padding>

    <Text id="welcome-text" text="Welcome" 
         GridPane.columnIndex="0" GridPane.rowIndex="0"
        GridPane.columnSpan="2"/>

    <Label text="User Name:"
        GridPane.columnIndex="0" GridPane.rowIndex="1"/>

    <TextField 
        GridPane.columnIndex="1" GridPane.rowIndex="1"/>

    <Label text="Password:"
        GridPane.columnIndex="0" GridPane.rowIndex="2"/>

    <PasswordField fx:id="passwordField" 
        GridPane.columnIndex="1" GridPane.rowIndex="2"/>

    <HBox spacing="10" alignment="bottom_right" 
        GridPane.columnIndex="1" GridPane.rowIndex="4">
        <Button text="Sign In"     
        onAction="#handleSubmitButtonAction"/>
    </HBox>

    <Text fx:id="actiontarget"
        GridPane.columnIndex="1" GridPane.rowIndex="6"/>

    <stylesheets>
      <URL value="@Login.css" />
    </stylesheets>

</GridPane>

答案 1 :(得分:3)

您对Class.getResource(String)的调用会查看资源的类路径(除非您已经使用类加载器做了一些时髦的事情),因此永远不会找到绝对路径。

如果您想从绝对路径加载文件,只需使用以下路径创建java.io.FileInputStream

String fxmlpath = "C:\\plugin\\pluginfxml.fxml";
Parent root = FXMLLoader.load(new FileInputStream(fxmlpath));

那就是说,我要小心绝对路径,它们不是非常便携 - 为应用程序创建标准目录结构然后将必要的目录添加到应用程序中更为常见类路径。

例如,您的应用程序可能具有如下目录结构:

My Application
  + bin
  + conf
  + lib
  + plugins

然后假设您从bin目录运行应用程序,您将使用如下类路径:

../conf;../plugins;../lib/*

允许您在应用程序中执行以下操作:

String fxmlpath = "pluginfxml.fxml";
Parent root = FXMLLoader.load(Class.getResourceAsStream(fxmlpath));