0

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.

froggyalex
  • 33
  • 7
  • Not a good idea, look into the MVC paradigm (Model View Controller). – Joop Eggen Jun 09 '21 at 12:23
  • @JoopEggen thanks for the suggestion. So in my case, the View is MainApp and Model is... a third class I'd need to create? But how would I access things from that third class in MainApp and/or MainController ? And how would I create Interface elements using "code"? The reason I made the code above is that the number of created Cells depends on the size of the boardgame that I want, and I want to keep the possibility to have whatever length or width – froggyalex Jun 09 '21 at 12:32
  • If you have a `MainController` class then I assume that means you also have a FXML file. (Please don't tell me that you are using [Scene Builder](https://gluonhq.com/products/scene-builder/)) Personally, I try to put as much as I can into the FXML file and only put in the controller class the things that can't be done in FXML. Refer to [Introduction to FXML](https://docs.oracle.com/javase/8/javafx/api/javafx/fxml/doc-files/introduction_to_fxml.html) – Abra Jun 09 '21 at 12:36
  • @Abra I absolutely am using Scene Builder... Is something wrong about doing so? I did try to put as much as possible in the FXML file, but since the board "representation" is variable (it can be 5x10 or 25x13 etc) I couldn't find a way to put it in there. Every other Pane, Label, Button is in the FXML file indeed – froggyalex Jun 09 '21 at 12:38
  • 1
    Yes, the model is a separate class. You create an instance of the model and share it with anything else that needs it. IMO if you're using FXML, then you're really using more of a MVP ("passive view") pattern; the "controller" is the presenter. Then the model instance typically gets shared with any controllers that need it. See if https://stackoverflow.com/questions/32342864/applying-mvc-with-javafx and https://stackoverflow.com/questions/36868391/using-javafx-controller-without-fxml help. – James_D Jun 09 '21 at 12:53
  • The FXML "Controller" is definitely not a Controller in the MVC sense. The {FXML + Controller} together comprise the "View" in MVC. – DaveB Jun 09 '21 at 14:11
  • 1
    The common element in all of the variants of these patterns (MVC, MVP, MVVM, etc.) is a separate model with no knowledge of the view that supports observability. The different patterns vary in the way they divide responsibilities between the "view" and "controller" (or presenter, etc.) and the relationship between those entities. But an observable model is fundamental to all these patterns. – James_D Jun 09 '21 at 14:25

0 Answers0