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>
I'm Trying all solution and answers that already answered in stack overflow. but issue is not solved.