javaFX-scene builder button event doesn't work

dancer_69 picture dancer_69 · Apr 8, 2017 · Viewed 12.6k times · Source

I'm trying from yesterday javaFX and scene builder in a very simple application to get button click to work, but everything I tried(by following some tutorials or related answers here) it doesn't work. I created a new javaFX project and these are the files created with my editings:

Main.java:

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

 public class Main extends Application {

     @Override
     public void start(Stage primaryStage) {
         try {
            FXMLLoader loader = new 
        FXMLLoader(getClass().getResource("sample.fxml"));
        loader.setController(new SampleController());
        Pane root = loader.load();
            Scene scene = new Scene(root,400,400);
            scene.getStylesheets().add(getClass().getResource
            ("application.css").toExternalForm());
            primaryStage.setScene(scene);
            primaryStage.show();
         } catch(Exception e) {
            e.printStackTrace();
         }
    }

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

SampleControler.java:

package application;

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.scene.control.Label;

public class SampleController implements Initializable{

    @FXML
    Button bOk;
    @FXML
    Label lbTest;

    private void handleButtonAction(ActionEvent event) {
         // Button was clicked, do something...
         lbTest.setText("Button Action\n");
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        // TODO Auto-generated method stub
        bOk.setOnAction(this::handleButtonAction);
    }

}

sample.fxml:

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.Pane?>


<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" 
    minWidth="-Infinity" prefHeight="220.0" prefWidth="484.0" 
    xmlns="http://javafx.com/javafx/8.0.111" 
    xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <Button id="bOk" layoutX="214.0" layoutY="152.0" 
    mnemonicParsing="false" text="Button" />
      <Label id="lbTest" layoutX="214.0" layoutY="57.0" prefHeight="25.0" 
    prefWidth="138.0" text="Label" />
   </children>
</Pane>

The above doesn't give any error, but when button clicked it doesn't do anything(supposed to change Label' s text). I tried to set onAction on fxml file but this always gives error(cannot resolve onAction), no matter what I put there or if I do it from scene builder or by manually edit the fxml file(I have created the method and wrote it correct in onAction). What I'm doing wrong?

The error I'm getting now:

    javafx.fxml.LoadException: 
/home/workspace/testapp/bin/application/sample.fxml

    at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
    at application.Main.start(Main.java:16)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
    at com.sun.glass.ui.gtk.GtkApplication.lambda$null$49(GtkApplication.java:139)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException
    at application.SampleController.initialize(SampleController.java:27)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)

After I used fx:id instead of id for button and label the button worked as expected and the above errors gone.

Answer

fabian picture fabian · Apr 8, 2017

You forgot to use the controller with the fxml:

<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" 
    minWidth="-Infinity" prefHeight="220.0" prefWidth="484.0" 
    xmlns="http://javafx.com/javafx/8.0.111" 
    xmlns:fx="http://javafx.com/fxml/1"
    fx:controller="application.SampleController">

or

FXMLLoader loader = new FXMLLoader(getClass().getResource("sample.fxml"));
loader.setController(new SampleController());
Pane root = loader.load();

Furthermore you need to use fx:id instead of id attributes to inject objects to the controller:

<Button fx:id="bOk" layoutX="214.0" layoutY="152.0" 
        mnemonicParsing="false" text="Button" />
<Label fx:id="lbTest" layoutX="214.0" layoutY="57.0" prefHeight="25.0" 
       prefWidth="138.0" text="Label" />