1

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>
4
  • You shouldn't need to edit the FXML file directly. SceneBuilder allows you to specify a controller, component ids and actions. Did you specify the action via SceneBuilder? Commented Sep 14, 2017 at 18:06
  • No I didn't, I wrote into it in Eclipse Commented Sep 14, 2017 at 18:22
  • Sorry, I misread your comment. Should definitely use SceneBuilder for that. You should almost never have to edit the FXML file directly when you're using SceneBuilder. Commented Sep 14, 2017 at 18:29
  • SceneBuilder can also generate the skeleton code for the controller, once you have added fx:ids and event handlers, and specified a controller class. Go to "View" on the menu and choose "Show skeleton controller code". Then you can copy and paste the controller code into your IDE (the controller must be edited in an IDE, not in SceneBuilder). Commented Sep 14, 2017 at 18:43

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.