1

I am trying to fire existing button click event by KeyPress events. It only fails when controls is made by FXML. For example, this sample does work fine. But when I make almost same view and controls with FXML, and KeyPressed event fire button click event, a button turns null. Why is this happening? What did I forget, something basic?

A KeyPress event work, but when fire button event it throws NullpointerException.

This is simple sample java code.

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.stage.Stage;

import java.io.IOException;

public class RapidFireFXMLEdition extends Application {

    @FXML
    private Button button;
    @FXML
    private Label label;

    private static int nClicks = 0;

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

    @Override
    public void start(Stage primaryStage) throws IOException{
        Parent root = FXMLLoader.load(getClass().getResource("rapidfire.fxml"));
        Scene scene = new Scene(root);

        scene.setOnKeyPressed(event -> handleKeyPressed(event));
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private void handleKeyPressed(KeyEvent event) {
        KeyCode keyCode = event.getCode();
        System.out.println("handleKeyPressed, keyCode: " + keyCode);
        if (keyCode == KeyCode.F1) {
            button.fire();  // button turns null 
        }

    }

    @FXML
    private void handleClick() {
        nClicks++;
        System.out.println("Clicked " + nClicks + " times.");
        label.setText("Clicked " + nClicks + " times.");
    }   
}

And fxml file.

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.VBox?>

<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="90.0" prefWidth="100.0" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="rapid_sample.RapidFireFXMLEdition">
   <children>
      <Button fx:id="button" mnemonicParsing="false" onAction="#handleClick" text="Click Me!" />
      <Label fx:id="label" text="display count here" />
      <BorderPane prefHeight="200.0" prefWidth="400.0" />
   </children>
</VBox>

updated:

Here is just memo taking answers. I make it possible that KeyPressd event fire click event with FXML, and I devide a controller from Application class.

RapidCleanApp.java:

package rapid_sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

import java.io.IOException;

public class RapidCleanApp extends Application {

    public static Stage primaryStage;
    private BorderPane rootLayout;

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

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

        this.primaryStage = primaryStage;
        this.primaryStage.setTitle("Rapid but Clean App");
        initRootLayout();
        this.primaryStage.show();
    }

    private void initRootLayout() throws IOException {
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(RapidCleanApp.class.getResource("rapidclean.fxml"));
        rootLayout = loader.load();
        Scene scene = new Scene(rootLayout);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

}

RapidCleanAppController.java:

package rapid_sample;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.BorderPane;

import java.net.URL;
import java.util.ResourceBundle;

public class RapidCleanAppController implements Initializable{

    @FXML
    private BorderPane pane;
    @FXML
    private Button button;
    @FXML
    private Label label;

    private static int nClicks = 0;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        System.out.println("Controller is initializing");
    }

    @FXML
    private void handleKeyPressed(KeyEvent event) {
        KeyCode keyCode = event.getCode();
        System.out.println("handleKeyPressed, keyCode: " + keyCode);
        if (keyCode == KeyCode.F1) {
            button.fire();
        }
    }

    @FXML
    private void handleClick() {
        nClicks++;
        System.out.println("Clicked " + nClicks + " times.");
        label.setText("Clicked " + nClicks + " times.");
    }

}

rapidclean.fxml:

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.VBox?>

<BorderPane fx:id="pane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" onKeyPressed="#handleKeyPressed" prefHeight="100.0" prefWidth="200.0" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1"
            fx:controller="rapid_sample.RapidCleanAppController">
   <center>
      <VBox prefHeight="200.0" prefWidth="100.0" BorderPane.alignment="CENTER">
         <children>
            <Button fx:id="button" mnemonicParsing="false" onAction="#handleClick" prefHeight="23.0" prefWidth="96.0" text="Click Me!" />
            <Label fx:id="label" text="display count here" />
         </children>
      </VBox>
   </center>
</BorderPane>
Community
  • 1
  • 1
ultrakanji
  • 13
  • 5

2 Answers2

0

I am not sure if Application class can be also a controller. Better move Controller to separetad class. Or other, ugly option is: Add:

public Button getButton() {
    return this.button;
}

then before scene.setOnKeyPressed:

RapidFireFXMLEdition controller = loader.getController();
this.button = controller.getButton();
user3718614
  • 530
  • 1
  • 5
  • 11
0

I wouldn't recommend using the Application class as controller; IMHO the Application class should only "assemble" the different parts, not contain much logic, since reusing the code in a Application class is somewhat limited by the application lifecycle. There are still plenty of ways to communicate with the controller class, as described here: Passing Parameters JavaFX FXML

However, if you use the Application class as controller, you should be aware of the fact that using fx:controller will create a new instance, not use the existing one. To use the existing one, you need to specify the controller instance when loading the scene and remove the fx:controller attribute in the fxml:

FXMLLoader loader = new FXMLLoader(getClass().getResource("rapidfire.fxml"));
loader.setController(this);
Parent root = loader.load();
...
<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="90.0" prefWidth="100.0" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1">
...
Community
  • 1
  • 1
fabian
  • 80,457
  • 12
  • 86
  • 114
  • Thank you. I will upload clean example code later. And now I can understand why I need to `get` and `retrun` a button object. – ultrakanji Feb 18 '16 at 10:07