Java FXML从绝对路径加载(动态) [英] Java FXML load (Dynamic) from absolute path
问题描述
我想从绝对路径或jar系统外的路径加载fxml文件。
I want to load fxml files from an absolute path or a path outside of my jar system.
background:
它将是一个简单的插件 - 系统看起来在所有fxml文件(后来的jar文件)的plugin文件夹中,并自动包含在TabPane中。
background: it will be a simple plugin-system that look's in the plugin folder for all fxml files (later jar files) and include it automatically in a 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 路径 class获取Path,然后将其转换为URL。如果程序没有对文件位置的读访问权限,则会出现唯一的问题。'
FXMLLoader takes in a URL as its argument, so we just use the NIO Paths class to get the Path, then convert it to a URL. The only problem occurs if the program does not have read access to the file's location.'
我用JavaFX教程中的示例代码充实了示例:
I've fleshed out the example with the example code from the JavaFX tutorial:
测试应用程序:
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>
这篇关于Java FXML从绝对路径加载(动态)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!