Home:ALL Converter>I am problems getting the scene for a Java FX menu item

I am problems getting the scene for a Java FX menu item

Ask Time:2020-06-21T13:04:06         Author:Flash Jack From Gundagai

Json Formatter

I am working on a java assignment and so I am not quite a "Jedi Master" when it comes Java, and in fact more at the "trainer wheels stage", and so still coming to grips with Java FX stages and scenes.. anyway here is a some prototype code that shows the problem I am having...

[ These all reside in the testingMenuAction package]

My main class: " testingMenuAction_main.java"

package testingMenuAction;

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

public class testingMenuAction_main extends Application {
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("/testingMenuAction/testingMenuAction.fxml"));
        Scene scene = new Scene(root);

    public static void main(String[] args) {

My main Java FXML file: "testingMenuAction.fxml"

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

<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.VBox?>

<AnchorPane fx:id="testingMenuActionPage" prefHeight="195.0" prefWidth="494.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="testingMenuAction.testingMenuActionController">
      <VBox prefHeight="186.0" prefWidth="493.0">
                <Menu mnemonicParsing="false" text="Data">
                    <MenuItem fx:id="mnu_data_import" mnemonicParsing="false" text="Import File" />

My controller class for my Java FXML file: "testingMenuActionController.java"

package testingMenuAction;

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

public class testingMenuActionController implements Initializable {

    private AnchorPane testingMenuActionPage;
    private MenuItem mnu_data_import;
    //this tests the operation of the alert dialog
   //private Stage export_chooser_stage;

     * This method will initialiise the UI
    public void initialize(URL url, ResourceBundle rb) 
         * This  is the mnu_data_import.setOnAction((event)
            mnu_data_import.setOnAction((event) -> 
                System.out.println("You clicked to Import Data");
                FileChooser fileChooser = new FileChooser();
                fileChooser.setTitle("Open Resource File");
                 new ExtensionFilter("Text Files", "*.csv")  );
                File selectedFile = fileChooser.showOpenDialog(  mnu_data_import.getScene().getWindow()  );
                String FileNamePath = selectedFile.toString();
                System.out.println("File name and path to file :" + FileNamePath);
            });// close
    }// close public void initialize(URL url, ResourceBundle rb)     

}// close public class testingMenuActionController

The compiler is complaining with the following instruction:

"File selectedFile = fileChooser.showOpenDialog(  mnu_data_import.getScene().getWindow()  );"

Giving the following error message... "The method getScene() is undefined for the type MenuItem"

Unfortunately my experinece with Java Windows, Stage, Scenes, and Nodes is still small [as I still on my "trainer wheels.."]...

As a result, would some one be able to help me?

Author:Flash Jack From Gundagai,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/62494594/i-am-problems-getting-the-scene-for-a-java-fx-menu-item
WonkOn :

There is no such method getScene() for the class MenuItem, see this documentation page.\nI guess you are trying to find the owner window so you can pass it to the showOpenDialog() method. What you can do is indeed use the method getScene(), not on your MenuItem (since it does not have a getScene() method) but rather on your root element, in this case testingMenuActionPage. (Note: it can be called on any element inheriting the class Node)\nmnu_data_import.setOnAction((event) ->\n{\n System.out.println("You clicked to Import Data");\n FileChooser fileChooser = new FileChooser();\n fileChooser.setTitle("Open Resource File");\n fileChooser.getExtensionFilters().addAll(new ExtensionFilter("Text Files", "*.csv") );\n File selectedFile = fileChooser.showOpenDialog(testingMenuActionPage.getScene().getWindow());\n String FileNamePath = selectedFile.toString();\n System.out.println("File name and path to file :" + FileNamePath);\n});\n",