【问题标题】:Problem getting fileChooser to work on a stage, not sure how to set the proper stage?让 fileChooser 在舞台上工作时出现问题,不确定如何设置正确的舞台?
【发布时间】:2020-06-20 03:12:27
【问题描述】:

我是 JavaFx 和在当前阶段使用文件选择器的新手 获取文件的文件名和路径。

我已经创建了测试代码来显示我遇到的问题......

test_main.java

package view;

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


public class test_main extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("/view/Sample.fxml"));

        Scene scene = new Scene(root);

        stage.setScene(scene);
        stage.show();
    }


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

这是我在同一个 View 包中的 sample.fxml 页面:

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

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

<AnchorPane fx:id="sample_page" prefHeight="681.0" prefWidth="503.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="view.SampleController">
  <children>

      <Button fx:id="btn_import_file" layoutX="403.0" layoutY="532.0" mnemonicParsing="false" text="Import File" />
  </children>
</AnchorPane>

这是我的 SampleController.java 类:

package view;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage; 
import javafx.stage.FileChooser;
import java.io.File;



public class SampleController implements Initializable {



    @FXML
    private AnchorPane samplePage;

    @FXML
    private Button btn_test_dialog;
    //this tests the operation of the alert dialog

   //private Stage export_chooser_stage;


    @FXML
    private Button export_posts_all;
    // this tests the file path select


    @FXML
    private Button btnImportFile;
    // this tests the import file path and name.





    //===================================================================================================================
    /*
     * This method will initialiise the UI
     */

    @Override
    public void initialize(URL url, ResourceBundle rb) 
    {
        //--------------------------------------------------------------------------------------------------------------------
        /*
         * This  is the  btn_import_file.setOnAction((event)
         */
        btnImportFile.setOnAction((event) -> 
        {
            Stage primaryStage=(Stage) samplePage.getScene().getWindow();
            //Stage primaryStage=new Stage();
            primaryStage.setTitle("JavaFX App");

            FileChooser fileChooser = new FileChooser();

            Button button = new Button("Select File");
            button.setOnAction(e -> {
                File selectedFile = fileChooser.showOpenDialog(primaryStage);
                String FileNamePath=selectedFile.toString();
                System.out.println("File name and path to file :"+FileNamePath); 
            });

            //VBox vBox = new VBox(button);
            //Scene scene = new Scene(vBox, 960, 600);

            //primaryStage.setScene(sample_page.getScene());
            primaryStage.getScene();
            primaryStage.show();
        });
        // this closes the  btn_test_dialog.setOnAction((event) ->  event


    }// close public void initialize(URL url, ResourceBundle rb)     



}// close public class SampleController implements Initializable 

会发生什么:

1.页面加载并显示“导入文件”按钮

  1. 当一个 onClick 的“导入文件”没有任何反应。

点击“导入文件”时的预期行为应该是:

  1. 操作系统文件选择对话框应出现在与“导入文件”按钮相同的阶段上方。 选择文件和路径应在控制台中打印文件名和文件路径。

  2. 显然我的代码存在问题,因为我是 Java FX 的新手。

有人可以帮助我吗?

【问题讨论】:

  • 需要一个最小的可重现示例...无法运行,您的 fx:id 标记与控制器中的内容不匹配...如果您希望人们遵循 Java 命名约定愿意花时间帮助你。文件/类应该大写,没有_下划线。方法/字段/属性应该是没有_下划线的驼峰式
  • 为什么按下“导入文件”时要创建一个新按钮?你永远不会显示那个按钮,所以用户永远没有机会按下它,所以它的处理程序(打开文件选择器)永远不会被执行。只需在“导入文件”按钮处理程序中直接显示文件选择器。
  • 谢谢你,但我同意 Jame_D 关于为什么我需要创建另一个按钮,因为它让我感到困惑......但虽然我正在使用按钮操作对其进行原型设计,但在实际应用程序中这个 fileChooser 实际上是从一个菜单项启动的......如何在不添加按钮的情况下为菜单项重新编码这个控件类?

标签: javafx filechooser jfxpanel


【解决方案1】:

修复您的Sample.fxml 文件,使fx:id 匹配控制器中的内容...更改此内容:

<AnchorPane fx:id="sample_page" prefHeight="681.0" prefWidth="503.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="view.SampleController">
  <children>

      <Button fx:id="btn_import_file" layoutX="403.0" layoutY="532.0" mnemonicParsing="false" text="Import File" />

到这里:

<AnchorPane fx:id="samplePage" prefHeight="681.0" prefWidth="503.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="view.SampleController">
  <children>

      <Button fx:id="btnImportFile" layoutX="403.0" layoutY="532.0" mnemonicParsing="false" text="Import File" />

在您的控制器中,您永远不会将新按钮添加到视图中,因此您永远不会点击它的 ActionEvent

像这样修改你的控制器,

        btnImportFile.setOnAction((event) -> {
            // Get the window for the current view to pass to the
            // FileChooser showOpenDialog()
            Window      primaryStage = samplePage.getScene().getWindow();
            FileChooser fileChooser  = new FileChooser();

            Button button = new Button("Select File");
            // Add your new button to this view
            // What will you do with this button after?  How will you dispose of it?
            samplePage.getChildren().add(button);
            button.setOnAction(e -> {
                File   selectedFile = fileChooser.showOpenDialog(primaryStage);

                // This will NPE if user cancels showOpenDialog.. 
                // and shpuld be `fileNamePath`
                // and I think you want `selectedFile.getPath()`
                // or `selectedFile.getAbsolutePath()`
                String FileNamePath = selectedFile.toString();
                System.out.println("File name and path to file :" + FileNamePath);
            });
        });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-08
    • 2012-11-22
    • 2014-05-12
    • 1970-01-01
    • 2013-12-04
    • 2020-09-30
    • 2018-12-17
    相关资源
    最近更新 更多