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;
}
}
}