0

I'm using Scene Builder interface and linking that through a fxml doc into Eclipse. My game background and grid pane/buttons are showing up fine. But how do I get the buttons to work? I've spend days researching this, although I'm sure it's pretty simple! Basically, all I want it to do is show the number 1 when I click on the button. My game engine in itself works fine and the number shows on the grid for the TEXT UI. It's just the GUI that's got me.

public class MyController {
private Maze game = new Maze();
private Label messages = new Label();
// private int moves = 0;

public void handleStart(ActionEvent event) {
    System.out.println("Welcome to The Maze.You must find all"
            + "+ the squares with zeros in them to make it out of the maze.");
}
public void handleHelp(ActionEvent event) {
    System.out.println("click on the ?, if you click on a square that is not 'zero'"
            + " you die. To win you must click on all the squares that are 'zero'. Good Luck! ");
}
@FXML
public void handleClick(ActionEvent event) {
    Button c = (Button) event.getSource();
    String id = c.getId();
    int row = Integer.parseInt(id.substring(6, 7));
    int col = Integer.parseInt(id.substring(7, 8));
    System.out.println("click: + " + event + "row=" + row + "," + col);
    boolean good = this.game.move1(row, col);
    if (good) {
        this.messages.setText("Good move");
    } else {
        this.messages.setText("Bad move.  Try again...");
    }
    updateView();
}
@FXML
GridPane grid;
public void updateView() {
    for (int row = 0; row < 3; row++) {
        for (int col = 0; col < 6; col++) {
            String bid = "#button" + row + col;
            Button b = (Button) grid.lookup(bid);
            System.out.println("Found Button: " + b);
            // Cell cell = game.getCell(row, col);
            // c.setText("" + cell.getValue());
        }
    }
}

} And below is my GUI class:

public class GUI extends Application {
    private Maze game = new Maze();
    private int moves = 0;

    @Override // Override start method in Application Class
    public void start(Stage primaryStage) throws IOException {
    Parent root =FXMLLoader.load(getClass().getResource("themaze.fxml.fxml"));
    Scene scene = new Scene(root, 788, 522 );
    primaryStage.setResizable(false);
    primaryStage.setTitle("The Maze");
    primaryStage.setScene(scene);
    primaryStage.show();
}
public static void main(String[] args) {
    Application.launch(args);
}

} The FXML doc (It's pretty long):

<?xml version="1.0" encoding="UTF-8"?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="533.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="maze_GUI.MyController">
<children>
  <ImageView fitHeight="533.0" fitWidth="800.0" layoutX="200.0" layoutY="116.0" pickOnBounds="true" preserveRatio="true" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
     <image>
        <Image url="@images/maze.jpg.jpg" />
     </image>
  </ImageView>
  <GridPane layoutX="176.0" layoutY="155.0" prefHeight="327.0" prefWidth="561.0" AnchorPane.bottomAnchor="51.0" AnchorPane.leftAnchor="176.0" AnchorPane.rightAnchor="63.0" AnchorPane.topAnchor="155.0">
    <columnConstraints>
      <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
      <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
        <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
        <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
        <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
        <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
    </columnConstraints>
    <rowConstraints>
      <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
      <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
      <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
    </rowConstraints>
     <children>
        <Button fx:id="button25" alignment="CENTER" mnemonicParsing="false" onAction="#handleClick" prefHeight="121.0" prefWidth="93.0" style="-fx-border-color: white; -fx-border-width: 2; -fx-background-color: rgb(34, 139, 34);" text="?" textAlignment="CENTER" textFill="#fbf9f9" GridPane.columnIndex="5" GridPane.rowIndex="2">
           <font>
              <Font name="Century Schoolbook Bold" size="42.0" />
           </font>
        </Button>
        <Button fx:id="button15" alignment="CENTER" mnemonicParsing="false" onAction="#handleClick" prefHeight="121.0" prefWidth="93.0" style="-fx-border color: white; -fx-border-width: 2; -fx-background-color: rgb(34, 139, 34);" text="?" textAlignment="CENTER" textFill="#fbf9f9" GridPane.columnIndex="5" GridPane.rowIndex="1">
           <font>
              <Font name="Century Schoolbook Bold" size="42.0" />
           </font>
        </Button>
        <Button fx:id="button05" alignment="CENTER" mnemonicParsing="false" onAction="#handleClick" prefHeight="121.0" prefWidth="93.0" style="-fx-border color: white; -fx-border-width: 2; -fx-background-color: rgb(34, 139, 34);" text="?" textAlignment="CENTER" textFill="#fbf9f9" GridPane.columnIndex="5">
           <font>
              <Font name="Century Schoolbook Bold" size="42.0" />
           </font>
        </Button>
        <Button fx:id="button24" alignment="CENTER" mnemonicParsing="false" onAction="#handleClick" prefHeight="118.0" prefWidth="103.0" style="-fx-border color: white; -fx-border-width: 2; -fx-background-color: rgb(34, 139, 34);" text="?" textAlignment="CENTER" textFill="#fbf9f9" GridPane.columnIndex="4" GridPane.rowIndex="2">
           <font>
              <Font name="Century Schoolbook Bold" size="42.0" />
           </font>
        </Button>
        <Button fx:id="button14" alignment="CENTER" mnemonicParsing="false" onAction="#handleClick" prefHeight="122.0" prefWidth="113.0" style="-fx-border color: white; -fx-border-width: 2; -fx-background-color: rgb(34, 139, 34);" text="?" textAlignment="CENTER" textFill="#fbf9f9" GridPane.columnIndex="4" GridPane.rowIndex="1">
           <font>
              <Font name="Century Schoolbook Bold" size="42.0" />
           </font>
        </Button>
        <Button fx:id="button23" alignment="CENTER" mnemonicParsing="false" onAction="#handleClick" prefHeight="120.0" prefWidth="107.0" style="-fx-border color: white; -fx-border-width: 2; -fx-background-color: rgb(34, 139, 34);" text="?" textAlignment="CENTER" textFill="#fbf9f9" GridPane.columnIndex="3" GridPane.rowIndex="2">
           <font>
              <Font name="Century Schoolbook Bold" size="42.0" />
           </font>
        </Button>
        <Button fx:id="button22" alignment="CENTER" mnemonicParsing="false" onAction="#handleClick" prefHeight="121.0" prefWidth="93.0" style="-fx-border-color: white; -fx-border-width: 2; -fx-background-color: rgb(34, 139, 34);" text="?" textAlignment="CENTER" textFill="#fbf9f9" GridPane.columnIndex="2" GridPane.rowIndex="2">
           <font>
              <Font name="Century Schoolbook Bold" size="42.0" />
           </font>
        </Button>
            <Button fx:id="button21" alignment="CENTER" mnemonicParsing="false" onAction="#handleClick" prefHeight="121.0" prefWidth="93.0" style="-fx-border-color: white; -fx-border-width: 2; -fx-background-color: rgb(34, 139, 34);" text="?" textAlignment="CENTER" textFill="#fbf9f9" GridPane.columnIndex="1" GridPane.rowIndex="2">
           <font>
              <Font name="Century Schoolbook Bold" size="42.0" />
           </font>
        </Button>
        <Button fx:id="button12" alignment="CENTER" mnemonicParsing="false" onAction="#handleClick" prefHeight="121.0" prefWidth="93.0" style="-fx-border-color: white; -fx-border-width: 2; -fx-background-color: rgb(34, 139, 34);" text="?" textAlignment="CENTER" textFill="#fbf9f9" GridPane.columnIndex="2" GridPane.rowIndex="1">
           <font>
              <Font name="Century Schoolbook Bold" size="42.0" />
           </font>
        </Button>
        <Button fx:id="button11" alignment="CENTER" mnemonicParsing="false" onAction="#handleClick" prefHeight="121.0" prefWidth="93.0" style="-fx-border-color: white; -fx-border-width: 2; -fx-background-color: rgb(34, 139, 34);" text="?" textAlignment="CENTER" textFill="#fbf9f9" GridPane.columnIndex="1" GridPane.rowIndex="1">
           <font>
              <Font name="Century Schoolbook Bold" size="42.0" />
           </font>
        </Button>
        <Button fx:id="button20" alignment="CENTER" mnemonicParsing="false" onAction="#handleClick" prefHeight="121.0" prefWidth="93.0" style="-fx-border-color: white; -fx-border-width: 2; -fx-background-color: rgb(34, 139, 34);" text="?" textAlignment="CENTER" textFill="#fbf9f9" GridPane.rowIndex="2">
           <font>
              <Font name="Century Schoolbook Bold" size="42.0" />
           </font>
        </Button>
        <Button fx:id="button10" alignment="CENTER" mnemonicParsing="false" onAction="#handleClick" prefHeight="151.0" prefWidth="93.0" style="-fx-border-color: white; -fx-border-width: 2; -fx-background-color: rgb(34, 139, 34);" text="?" textAlignment="CENTER" textFill="#fbf9f9" GridPane.rowIndex="1">
           <font>
              <Font name="Century Schoolbook Bold" size="42.0" />
           </font>
        </Button>
        <Button fx:id="button13" alignment="CENTER" mnemonicParsing="false" onAction="#handleClick" prefHeight="114.0" prefWidth="111.0" style="-fx-border-color: white; -fx-border-width: 2; -fx-background-color: rgb(34, 139, 34);" text="?" textAlignment="CENTER" textFill="#fbf9f9" GridPane.columnIndex="3" GridPane.rowIndex="1">
           <font>
              <Font name="Century Schoolbook Bold" size="42.0" />
           </font>
        </Button>
        <Button fx:id="button04" alignment="CENTER" mnemonicParsing="false" onAction="#handleClick" prefHeight="109.0" prefWidth="114.0" style="-fx-border-color: white; -fx-border-width: 2; -fx-background-color: rgb(34, 139, 34);" text="?" textAlignment="CENTER" textFill="#fbf9f9" GridPane.columnIndex="4">
           <font>
              <Font name="Century Schoolbook Bold" size="42.0" />
           </font>
        </Button>
        <Button fx:id="button03" alignment="CENTER" mnemonicParsing="false" onAction="#handleClick" prefHeight="116.0" prefWidth="114.0" style="-fx-border-color: white; -fx-border-width: 2; -fx-background-color: rgb(34, 139, 34);" text="?" textAlignment="CENTER" textFill="#fbf9f9" GridPane.columnIndex="3">
           <font>
              <Font name="Century Schoolbook Bold" size="42.0" />
           </font>
        </Button>
        <Button fx:id="button02" alignment="CENTER" mnemonicParsing="false" onAction="#handleClick" prefHeight="121.0" prefWidth="93.0" style="-fx-border-color: white; -fx-border-width: 2; -fx-background-color: rgb(34, 139, 34);" text="?" textAlignment="CENTER" textFill="#fbf9f9" GridPane.columnIndex="2">
           <font>
              <Font name="Century Schoolbook Bold" size="42.0" />
           </font>
        </Button>
        <Button fx:id="button01" alignment="CENTER" mnemonicParsing="false" onAction="#handleClick" prefHeight="121.0" prefWidth="93.0" style="-fx-border-color: white; -fx-border-width: 2; -fx-background-color: rgb(34, 139, 34);" text="?" textAlignment="CENTER" textFill="#fbf9f9" GridPane.columnIndex="1">
               <font>
                  <Font name="Century Schoolbook Bold" size="42.0" />
               </font>
            </Button>
            <Button fx:id="button00" alignment="CENTER" mnemonicParsing="false" onAction="#handleClick" prefHeight="139.0" prefWidth="93.0" style="-fx-border color: white; -fx-border-width: 2; -fx-background-color: rgb(34, 139, 34);" text="?" textAlignment="CENTER" textFill="#fbf9f9">
               <font>
                  <Font name="Century Schoolbook Bold" size="42.0" />
               </font>
            </Button>
         </children>
      </GridPane>
   </children>
</AnchorPane>
AVW
  • 15
  • 4
  • There are two ways to handle a button created in FXML. One is to get the button reference by using `@FXML Button buttonFXID` and adding a `buttonFXID.setOnAction(...)`. The other way is to used a button handling method that starts like `@FXML public void handleClick(ActionEvent event) {..}`. In the first example set the buttons fxid in Scenebuilder also. In the second example, add `handleClick` or the name of your handler method to the button's onAction in SceneBuilder. – SedJ601 Jun 02 '17 at 02:58
  • Post your FXML file. SceneBuilder basically alters your FXML file for you. – SedJ601 Jun 02 '17 at 03:02
  • Sedrick Jefferson, I've already added handleClick to the buttons onAction in SceneBuilder, this is what's confusing me a little. It should work? I'll post the FXML file too. – AVW Jun 02 '17 at 03:26
  • You should add `@FXML` before your method. Look at the handleClick in my comment above. – SedJ601 Jun 02 '17 at 03:28
  • Just did that, but it's throwing a Target or a null pointer exception – AVW Jun 02 '17 at 03:42
  • What line is throwing the null pointer exception? – SedJ601 Jun 02 '17 at 04:21
  • I am going to guess it's this one: `Button b = (Button) grid.lookup(bid);` – SedJ601 Jun 02 '17 at 04:22
  • Yes! And at updateView() – AVW Jun 02 '17 at 04:26
  • I am not sure if you can look up the fx:id like this? – SedJ601 Jun 02 '17 at 04:26
  • https://stackoverflow.com/questions/20655024/javafx-gridpane-retrive-specific-cell-content – SedJ601 Jun 02 '17 at 04:29
  • I tried that, wasn't quite working, but I've called a print statement for the id and it's referencing the correct cell. – AVW Jun 02 '17 at 04:50
  • Unfortunately, I am off to bed. If you still having this problem tomorrow, I will attempt to run your code. – SedJ601 Jun 02 '17 at 04:52
  • Running through the debugger, the first thing it gives is a QuantumToolkit.class tab that says "source not found". I'm also printing the line: ("click:" +event+ "row=" +row+ "," +col) and it is printing the correct info – AVW Jun 02 '17 at 04:56
  • That's fine. Thanks anyway :) – AVW Jun 02 '17 at 04:56
  • I am amazed how your controller didn't throw exception at runtime with `@FXML GridPane grid;` because I cannot find that grid in your FXML. – Jai Jun 02 '17 at 05:24
  • The full FXML isn't posted. It was too long – AVW Jun 02 '17 at 05:29
  • My apologies, it is all up there. What do you mean by it not being in my FXML? And how do I fix it? – AVW Jun 02 '17 at 05:30
  • Every `@FXML field` in controller must correspond to a control with the same `fx:id` in the FXML. If your current FXML is the complete one, then there is definitely no `GridPane` with `fx:id="grid"`. If you added a breakpoint at `Button b = (Button) grid.lookup(bid);`, then point your cursor over `grid` during the breakpoint, you should see that it is `null`. – Jai Jun 02 '17 at 05:49
  • nope, not yet. Yes, I'm still a beginner at Java, but tell that to a course coordinator demanding a working assignment. – AVW Jun 03 '17 at 07:24
  • James_D thank you for the link by the way, was a goldmine! – AVW Jun 03 '17 at 09:38

0 Answers0