Java FXML calling methods on button clicks

JPadley

I am creating a basic game launcher in JavaFX with SceneBuilder. Since SceneBuilder works in FXML, my launcher layout is in FXML. I have a method in my main class that I want to call on a button click. I read that you could use

#methodName

In the button's

onAction

property, but this does not work.

My main Java class:

@FXML
private void launchGame(ActionEvent e) {
    System.out.println("Launching...");
}

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

    primaryStage.setTitle("First Week Login");
    primaryStage.setResizable(false);
    primaryStage.sizeToScene();

    primaryStage.show();
}

My FXML file:

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.text.Text?>
<?import javafx.scene.web.WebView?>


<AnchorPane xmlns:fx="http://javafx.com/fxml/1" 
xmlns="http://javafx.com/javafx/8.0.102">
<children>
  <BorderPane prefHeight="493.0" prefWidth="664.0" styleClass="background" 
stylesheets="@launcher.css">
     <bottom>
        <HBox alignment="CENTER" prefHeight="100.0" prefWidth="200.0" 
BorderPane.alignment="CENTER">
           <children>
              <Button alignment="CENTER" mnemonicParsing="false" 
text="Launch Game" onAction="#launchGame" />
           </children>
        </HBox>
     </bottom>
     <top>
        <HBox alignment="CENTER" prefHeight="100.0" prefWidth="200.0" 
BorderPane.alignment="CENTER">
           <children>
              <Text strokeType="OUTSIDE" strokeWidth="0.0" 
styleClass="title" text="First Week" />
           </children>
        </HBox>
     </top>
     <center>
        <WebView prefHeight="200.0" prefWidth="200.0" 
BorderPane.alignment="CENTER" />
     </center>
  </BorderPane>
</children>
</AnchorPane>
Aleksandr

You need to create a separate Controller class and specify it in top AnchorPane tag with fx:controller="packageName.Classname"

Like this:

<AnchorPane xmlns:fx="http://javafx.com/fxml/1"
 xmlns="http://javafx.com/javafx/8.0.102"
 fx:controller="com.packageName.Controller">

The called method should be inside the specified Controller class.

com.packageName is just an example, you should use the name of the package where you put the Controller class or no package name if it's not in any package.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

different ways of calling class method with button clicks

From Dev

XPages - Calling Java methods

From Dev

Calling the methods in java

From Dev

Calling Java Methods in XSLT

From Java

Calling multiple methods in Java

From Java

Calling the methods in a class in Java

From Dev

Calling methods on objects Java

From Dev

Calling JQuery Button Clicks/Form Submit in Firebase Functions and Hosting

From Dev

c# button calling several methods

From

Android - Calling methods from notification action button

From Java

Calling methods on string literals (Java)

From Java

Calling methods of "parent" component in Java

From Java

Calling two Java methods asynchronously

From Dev

Calling Java methods with React locally

From Java

calling java methods in javascript code

From Dev

Calling methods in a Java Fx ActionEvent

From Dev

Generics in Java preventing calling of methods

From Dev

Calling methods for my java class

From Java

Chess GUI - Getting 2 user clicks before calling a funtion - Java

From Dev

Methods for detecting use of a button in java

From Dev

Clojure: calling a sequence of methods on a Java object

From Java

Java: Printing triangles and spaces by calling methods

From Dev

Programatically calling all methods in a Java class

From Dev

Compiler error with calling Generic methods java

From Dev

Calling methods of a Java subclass using Jython

From Java

Calling Java Methods from Shell Scripts

From Dev

calling objects from other methods in java

From Dev

Using variables for creating objects and calling methods in Java

From Dev

Calling Methods In Java - Hello World Example