[英]Java FXML load (Dynamic) from absolute path
我想從絕對路徑或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 ...
}
應該很簡單:
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>
您對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));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.