简体   繁体   English

Controller 类中的 FileChooser - SceneBuilder JavaFX

[英]FileChooser in Controller class - SceneBuilder JavaFX

I'm trying to code the functionality of a button created using SceneBuilder.我正在尝试编写使用 SceneBuilder 创建的按钮的功能。 I found the "Code" section in Scene builder and set the name of the method that will launch when I click on the button (eg fileSelector).我在场景构建器中找到了“代码”部分,并设置了单击按钮时将启动的方法的名称(例如 fileSelector)。 In the method I should use FileChooser variable(I need to take an image from the PC and save it to a "File" variable).在该方法中,我应该使用 FileChooser 变量(我需要从 PC 中获取图像并将其保存到“文件”变量中)。

I copied the sample controller skeleton and paste it to my Controller class.我复制了示例控制器骨架并将其粘贴到我的 Controller 类中。 Now I don't know how to implement the method, because I need a Stage variable to use with the FileChooser variable, but the stage variable is given for the public void start(Stage primaryStage) method.现在我不知道如何实现该方法,因为我需要一个 Stage 变量与 FileChooser 变量一起使用,但是该 stage 变量是为public void start(Stage primaryStage)方法提供的。

My Main Class我的主班

package application;

import java.io.IOException;

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

public class MainExample extends Application  {
    @Override
    public void start(Stage primaryStage) throws IOException {
        Pane firstPane = FXMLLoader.load(MainExample.class.getClassLoader().getResource("buttonExample.fxml"));
        Scene firstScene = new Scene(firstPane);
        primaryStage.setScene(firstScene);
        primaryStage.show();
    }

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

My Controller Class我的控制器类

package application;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;

public class MyController {

    @FXML
    private Button selectFile;

    @FXML
    void fileSelector(ActionEvent event) {
        FileChooser fileChooser = new FileChooser();
        File file = fileChooser.showOpenDialog(stage);
    }
}

The FXML from the button来自按钮的 FXML

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.Pane?>

<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="135.0" prefWidth="280.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.MyController">
    <children>
        <Button fx:id="selectFile" layoutX="102.0" layoutY="55.0" mnemonicParsing="false" onAction="#fileSelector" text="SelectFile" />
    </children>
</Pane>

You need a Window instance to show a FileChooser dialog, you can get it from the event like this:您需要一个Window实例来显示FileChooser对话框,您可以像这样从事件中获取它:

@FXML
void fileSelector(ActionEvent event) {
    Window window = ((Node) (event.getSource())).getScene().getWindow();
    FileChooser fileChooser = new FileChooser();
    File file = fileChooser.showOpenDialog(window);
    event.consume();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM