-2

I'm creating a JavaFX progema. But when i try to run the program its shows the location is not set error. I'm trying so many methods that showing in this platform. But the Error is not solved. So Please help me to clear this error. Thanks for advance.

MAIN

package com.mycompany.mavenproject1;

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

public class App extends Application {


    @Override
    public void start(Stage primaryStage) throws IOException {

        System.out.println(getClass().getResource("test.fxml"));
        FXMLLoader loader = new FXMLLoader(getClass().getResource("test.fxml"));
        Parent root = loader.load();
        System.out.println("roottt  "+root);
        TestController controller = loader.getController();
        Scene scene = new Scene(root);
        primaryStage.setScene(scene);
        controller.setStage(primaryStage);
        primaryStage.show();
    }

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

}

Controller

package com.mycompany.mavenproject1;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.stage.Stage;

public class TestController implements Initializable {

    @FXML
    private Button btn_close;
    private Stage stage;

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
    }    

    @FXML
    private void close_action(ActionEvent event) {
        stage.close();
    }

    void setStage(Stage stage) {
       this.stage = stage;  
    }
}

FXML

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

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


<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/20.0.1" fx:controller="com.mycompany.mavenproject1.TestController">
   <children>
      <Button fx:id="btn_close" layoutX="201.0" layoutY="166.0" mnemonicParsing="false" onAction="#close_action" prefHeight="25.0" prefWidth="112.0" text="X" />
   </children>
</AnchorPane>

Project Structure

I'm Trying all solution and answers that already answered in stack overflow. but issue is not solved.

Rashid
  • 1
  • 2
    That is not a standard maven project layout, [this is](https://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html). Java source goes under src/main/java and resources like fxml go under src/main/resources each placed in appropriate subdirectories for java packaging and resource lookup standards, as explained in the [eden guide](https://edencoding.com/where-to-put-resource-files-in-javafx/). – jewelsea Aug 16 '23 at 06:40
  • 1
    Edit the question, add the *complete* stack trace as text, formatted as code. – jewelsea Aug 16 '23 at 06:42
  • Provide the *exact* command that you use to run the program , so that your issue can be replicated. – jewelsea Aug 16 '23 at 06:46
  • @jewelsea I'm using netbeans IDE 18 and I just click the Run in IDE. I'm a beginner in this field. – Rashid Aug 16 '23 at 06:59
  • 3
    I recommend you read [Getting Started with JavaFX](https://openjfx.io/openjfx-docs/) and [How do I determine the correct path for FXML files, CSS files, Images, and other resources needed by my JavaFX Application?](https://stackoverflow.com/q/61531317/6395627). – Slaw Aug 16 '23 at 07:29
  • You have this statement in your code: `System.out.println(getClass().getResource("test.fxml"));`. What does it output? – jewelsea Aug 16 '23 at 17:34
  • If you don’t know how to find or read a stack trace so that you can provide one here, read [What is a stack trace?](https://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug-my-application-errors) – jewelsea Aug 16 '23 at 17:36
  • Off-topic: there is no need to manually pass the stage to the controller. You can just do `btnClose.getScene().getWindow().hide();` to close the window. – James_D Aug 16 '23 at 19:07

0 Answers0