0

I am constructing a Stage for my game in JavaFX. To assemble the contents of this stage, I am looking to assemble a series of panes that can be created and rearranged as needed, and I am creating a class for each pane. For instance, I have a class ScoreboardPane to contain the scoreboard and a class BoardPane to contain the game board and each of the game pieces. If I can create these pane classes to be independent of one another, rearranging them on a Stage through use of an AnchorPane should be trivial.

My issue stems from attempting to implement best code practices. Based on my research, it is recommended that I create these panes through use of FXML, creating controller classes for each one. So for a ScoreboardPane, I would end up with the ScoreboardPane class that has the code which creates a ScoreboardPane, a ScoreboardPaneController class, and a ScoreboardPane.fxml file. So if I understand, the ScoreboardPane would be the View and the ScoreboardPaneController and FXML file would be the Controller. Then I would have another class called Scoreboard to act as the Model and contain the business logic. The ScoreboardPaneController would be loaded in the MainWindowPane class that contains the ScoreboardPane.

To fit the MVC model, this setup makes sense, but what I'm seeing in practice is that the Controller is just acting as a buffer. This is because all of my panes are dependent on game data from the Model class to be created properly. For instance, in order to create a ScoreboardPane, I need to set the names of the players, which I can't set in the initialize() method since I have to pass the names to the controller after load() is called. So instead of creating a ScoreboardPane by calling ScoreboardPane(names), I'm creating the FXML class and controller and loading it, passing the names to the controller to accomplish the same goal.

It feels as though I'm taking all the variables I need to build a ScoreboardPane and transferring them to a designated "Pane Building Area" when I already have everything I need to build them within the ScoreboardPane itself. What is the significance in going through all of this extra code writing if my panes don't have anything for the controller to add? Is it purely for futureproofing the code?

FXML File

<?import game.gui.pane.*?>

<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>

<ScoreboardPane fx:id="sPane" 
fx:controller="game.controller.pane.ScoreboardPaneController" 
xmlns:fx="http://javafx.com/fxml">
</ScoreboardPane>

Controller File

package game.controller.pane;

import game.gui.pane.ScoreboardPane;
import javafx.fxml.FXML;

public class ScoreboardPaneController {

@FXML
ScoreboardPane sPane;

@FXML
String[] names;

@FXML
public void initialize()
{

}

@FXML
public void createPane()
{ 
    sPane.setNames(names); //Assigns names in ScoreboardPane class
}

public void setNames(String[] n)
{
    names = n;
}

}

MainWindowPane File

package game.gui.pane;

public class MainWindowPane
{
private String[] playerNames;

public MainWindowPane(GameData data)
{
     String[] names = data.getNames(); //Retrieved from model class
     ScoreboardPane scoreboard = createScoreboardPane(names);
}

private void createScoreboardPane(String[] names)
{
try
{
    FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/game/fxml/pane/ScoreboardPane.fxml")); 
    ScoreboardPane root = fxmlLoader.load();
        ScoreboardPaneController controller = fxmlLoader.<ScoreboardPaneController>getController();
    controller.setNames(names);
    controller.createPane();
    return root;
}
catch(Exception e)
{
    e.printStackTrace();
    return null;
}
}

}
MagerBlutooth
  • 23
  • 1
  • 3
  • What is the code in ScoreboardPane? (Also https://codereview.stackexchange.com might be a better place for a question like thins). One of the primary reasons to choose FXML is so that you can build the interface using a visual builder such as the JavaFX SceneBuilder (though there are other reasons). FXML is not appropriate will all things, so using it is a judgement call based upon your experience and requirements. You can also mix things if needed, creating some things in FXML and some in Java Code or scripts. – jewelsea Oct 30 '19 at 20:07
  • Consider [mvvmfx](https://github.com/sialcasa/mvvmFX) rather than pure MVC, especially if you are new to MVC and feel like you are trying to implement the pattern yourself. [Spring auto-wiring](https://stackoverflow.com/questions/57887944/adding-spring-dependency-injection-in-javafx-jpa-repo-service/57896435#57896435) or another dependency injection framework can be used for some of the setting of values that you are doing, but that is a more advanced topic, and I wouldn't necessarily advise starting with that if you don't already know it. – jewelsea Oct 30 '19 at 20:09
  • After working through the code some more, I've come to the conclusion that the ScoreboardPane itself is an unnecessary class. If I need to create a Scoreboard, I can simply call the ScoreboardPaneController to create it. For instances where I have objects within objects, I can call FXML from within the controller to load the sub-objects. With that setup specified, I would only have the one View class, that being the MainWindow that loads the main Pane, which would then load each individual component of the Pane. My goal is to have only the controller call model and view as needed. – MagerBlutooth Oct 31 '19 at 15:32

0 Answers0