I very recently started learning JavaFX by myself, and I have a hard time figuring out if what I'm doing is OK, or if it's considered bad practice.
So I'm currently trying to create an interface for a previous Java project I had (which only worked through console inputs and outputs). It's a Boardgame, using tiles called Cell
, Player
objects, etc.
At the moment, I have MainApp.java
, which creates the Stage and Scene, a Main.fxml
which declares most of the interface, and MainController.java
which does a number of things.
So first things it does is managing a few button clicks event, which is pretty standard I suppose.
But it also declares all the variables needed to start a game : creating Players and the board itself for instance. So first question : is that bad practice to put that in a Controller class?
Then another thing it does is actually create all the Panes needed to build the board interface.
Basically here's my code :
private void initBoardView() {
BoardGame board = theGame.getBoard(); /* The BoardGame Object is used by a Game object */
int boardY = board.getWidth();
int boardX = board.getLength();
for (int y = 0; y < boardY; y++) {
buttonsContainer = new GridPane(); /* I initialize the GridPane declared in the fxml file */
boardPane.getChildren().add(buttonsContainer);
for (int x = 0; x < boardX; x++) {
Cell cell = board.getCell(x, y); /* Cell are tiles, basically */
Button res = new Button(cell.display()); /* I create a Button for each Cell */
res.setId("button-"+cell.getId());
res.getStylesheets().add(getClass().getResource("/resources/css/Main.css").toExternalForm());
res.setOnAction(e -> handleCellClicked(e));
buttonsContainer.add(res,y,x);
}
}
}
My second question is : is that even supposed to be in a Controller class? I declare Buttons, css... I tried to figure out a way to do that elsewhere but I couldn't come up with a better solution.
I hope someone can shed some light.