Problem with JavaFX OnAction button
Published: 2023-03-02 19:42:53

Ciao a tutti,

ho un problema a cui non riesco a venirne a capo. Il problema nasce quando inserisco un OnAction nel bottone per fargli fare qualcosa quando viene premuto. Mi da una serie di errori che non riesco a capire e sto impazzendo. Spero che mi potete aiutare.

Vi ringrazio anticipatamente.


Main:

[CODE=java]package esercitazione.javafx;


import javafx.application.Application;

import javafx.fxml.FXMLLoader;

import javafx.scene.Parent;

import javafx.scene.Scene;

import javafx.stage.Stage;


public class Main extends Application {


    /**

     * @param args

     */

    public static void main(String[] args) {

        // TODO Auto-generated method stub

        launch(args);


    }


    @Override

    public void start(Stage primaryStage) throws Exception {

        // TODO Auto-generated method stub

        Parent root = FXMLLoader.load(getClass().getResource("home_convertitore.fxml"));

        primaryStage.setTitle("Convertitore Eur->USD");

        primaryStage.setScene(new Scene(root, 400, 200));

        primaryStage.show();

    

    }


}

[/CODE]


Controller:

[CODE=java]package esercitazione.javafx;


import javafx.event.ActionEvent;

import javafx.fxml.FXML;

import javafx.scene.control.Button;

import javafx.scene.control.Label;

import javafx.scene.control.TextField;


public class home_convertitoreController {


    @FXML

    private Button converti;


    @FXML

    private Label dollaro;


    @FXML

    private TextField euro;

 

    @FXML

    void btnConvert(ActionEvent event) {


    }


}

[/CODE]


FXML:

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


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

<?import javafx.scene.control.Label?>

<?import javafx.scene.control.TextField?>

<?import javafx.scene.layout.HBox?>

<?import javafx.scene.layout.VBox?>


<VBox alignment="CENTER" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/19" xmlns:fx="http://javafx.com/fxml/1">

   <children>

      <TextField fx:id="euro" promptText="EUR" />

      <Button fx:id="converti" mnemonicParsing="false" onAction="#btnConvert" text="Converti" />

      <HBox alignment="CENTER" prefHeight="100.0" prefWidth="200.0">

         <children>

            <Label fx:id="dollaro" />

            <Label text=" USD" />

         </children>

      </HBox>

   </children>

</VBox>

[/CODE]




Errore restituito:

[CODE]Exception in Application start method

java.lang.reflect.InvocationTargetException

    at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:119)

    at java.base/java.lang.reflect.Method.invoke(Method.java:578)

    at javafx.graphics@19.0.2.1/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:465)

    at javafx.graphics@19.0.2.1/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:364)

    at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)

    at java.base/java.lang.reflect.Method.invoke(Method.java:578)

    at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1081)

Caused by: java.lang.RuntimeException: Exception in Application start method

    at javafx.graphics@19.0.2.1/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:901)

    at javafx.graphics@19.0.2.1/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:196)

    at java.base/java.lang.Thread.run(Thread.java:1589)

Caused by: javafx.fxml.LoadException: No controller specified.

/C:/Users/XXX/eclipse-workspace/JavaFX_Esercitazione1/bin/esercitazione/javafx/home_convertitore.fxml:12


    at javafx.fxml@19.0.2.1/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2710)

    at javafx.fxml@19.0.2.1/javafx.fxml.FXMLLoader$Element.getControllerMethodHandle(FXMLLoader.java:568)

    at javafx.fxml@19.0.2.1/javafx.fxml.FXMLLoader$Element.processEventHandlerAttributes(FXMLLoader.java:610)

    at javafx.fxml@19.0.2.1/javafx.fxml.FXMLLoader$ValueElement.processEndElement(FXMLLoader.java:781)

    at javafx.fxml@19.0.2.1/javafx.fxml.FXMLLoader.processEndElement(FXMLLoader.java:2961)

    at javafx.fxml@19.0.2.1/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2646)

    at javafx.fxml@19.0.2.1/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2555)

    at javafx.fxml@19.0.2.1/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3368)

    at javafx.fxml@19.0.2.1/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3324)

    at javafx.fxml@19.0.2.1/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3292)

    at javafx.fxml@19.0.2.1/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3264)

    at javafx.fxml@19.0.2.1/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3240)

    at javafx.fxml@19.0.2.1/javafx.fxml.FXMLLoader.load(FXMLLoader.java:3233)

    at esercitazione.javafx.Main.start(Main.java:32)

    at javafx.graphics@19.0.2.1/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:847)

    at javafx.graphics@19.0.2.1/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:484)

    at javafx.graphics@19.0.2.1/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:457)

    at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)

    at javafx.graphics@19.0.2.1/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:456)

    at javafx.graphics@19.0.2.1/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)

    at javafx.graphics@19.0.2.1/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)

    at javafx.graphics@19.0.2.1/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184)

    ... 1 more

Exception running application esercitazione.javafx.Main

[/CODE]