0

I have been using Python and tkinter. I am now learning JavaFXML. I am having an issue accessing objects through controllers. I have a class named User. I create a User in my main Java class, the one that extends Application. The user has a location X and a location Y. When I push a button, I want to change the location of my user. I accomplish this easily when using JavaFX without FXML since everything is declared in the same class. But when I use FXML, I am dealing with a controller class which has no idea about the User class since I called it in my main class. I tried to initiate the User class in the controller, it seemed silly and didnt work. I also tried to search for a way to pass the user object to the controller but could not find a page on how to. Here is the code:

main class:

package simx;

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

public class SimX extends Application {
    User user;
    World world;
    Human human;

    public SimX() {
    }

    @Override
    public void start(Stage stage) throws Exception {

        user = new User(10, 10);

        Parent root = FXMLLoader.load(getClass().getResource("UIFXML.fxml"));

        Scene scene = new Scene(root);

        stage.setScene(scene);
        stage.show();

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

}

my FXML:

<?import javafx.scene.text.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane id="AnchorPane" prefHeight="480.0" prefWidth="640.0"     xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"     fx:controller="simx.UIFXMLController">
   <children>
      <GridPane layoutX="6.0" prefHeight="480.0" prefWidth="640.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>
        <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 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>
            <GridPane prefHeight="121.0" prefWidth="193.0" GridPane.columnSpan="2" GridPane.rowSpan="2">
               <columnConstraints>
                  <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" />
                  <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" />
                  <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" />
               </columnConstraints>
               <rowConstraints>
                  <RowConstraints minHeight="10.0" vgrow="SOMETIMES" />
                  <RowConstraints minHeight="10.0" vgrow="SOMETIMES" />
                  <RowConstraints minHeight="10.0" vgrow="SOMETIMES" />
               </rowConstraints>
               <children>
                  <Button fx:id="moveEastButton" layoutX="10.0" layoutY="38.0" onAction="#handleMoveEastButtonAction" prefHeight="25.0" prefWidth="500.0" text="Move East" GridPane.columnIndex="2" GridPane.rowIndex="1" />
                  <Button fx:id="moveSouthButton" layoutX="10.0" layoutY="38.0" onAction="#handleMoveSouthButtonAction" prefHeight="25.0" prefWidth="500.0" text="Move South" GridPane.columnIndex="1" GridPane.rowIndex="2" />
                  <Button fx:id="moveWestButton" layoutX="10.0" layoutY="38.0" onAction="#handleMoveWestButtonAction" prefHeight="25.0" prefWidth="500.0" text="Move West" GridPane.rowIndex="1" />
                  <Label alignment="CENTER" prefHeight="17.0" prefWidth="169.0" text="(X,Y)" GridPane.columnIndex="1" GridPane.rowIndex="1">
                     <font>
                    <Font size="24.0" />
                     </font>
                  </Label>
                    <Button fx:id="moveNorthButton" onAction="#handleMoveNorthButtonAction" prefHeight="25.0" prefWidth="500.0" text="Move North" GridPane.columnIndex="1" />
               </children>
            </GridPane>
         </children>
      </GridPane>
    </children>
</AnchorPane>

and my controller: /*

package simx;

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;

public class UIFXMLController implements Initializable {

@FXML
private Button moveNorthButton;

@FXML
private void handleMoveNorthButtonAction(ActionEvent event) {
    System.out.println("Move North");
    user.moveNorth();
}

@FXML
private Button moveSouthButton;

@FXML
private void handleMoveSouthButtonAction(ActionEvent event) {
    System.out.println("Move South");
}

@FXML
private Button moveEastButton;

@FXML
private void handleMoveEastButtonAction(ActionEvent event) {
    System.out.println("Move East");
}

@FXML
private Button moveWestButton;

@FXML
private void handleMoveWestButtonAction(ActionEvent event) {
    System.out.println("Move West");
}


@Override
public void initialize(URL url, ResourceBundle rb) {
}    

}

I dont think I need to show the User class.

1 Answers1

0

You may give the User instance to the Controller to manage it. To do this first

1) get the controller instance uiFXMLController like in Accessing FXML controller class.
2) uiFXMLController.setUser(user);
3) Update user's fields

@FXML
private void handleMoveEastButtonAction(ActionEvent event) {
    System.out.println("Move East");
    user.setLocationX( user.getLocationX() + 10 );
}
Community
  • 1
  • 1
Uluk Biy
  • 48,655
  • 13
  • 146
  • 153