0

So I'm learning JavaFX programming and MVC. The control is also its own class and isn't integrated into the view (Which I've heard is one way to go at it). I want it to be separated from the view but because I'm trying to encapsulate everything and leave everything private with limited access to the controls/nodes, I find myself using methods to do almost anything inside of my object almost entirely when using event handlers in the control.

Example (Not an actual program, just wrote it here because I have no short examples.):

View:

public class SamplePane extends BorderPane {

private TextField tfScoreOne;
private Button btnScore, btnPenalty;
private int scoreOne;

   public SamplePane() {
    // Some constructor
   }

   public void giveScore() {
      scoreOne++;
      tfScoreOne.textProperty().setValue("Score: " + Integer.toString(scoreOne);
   }

   public void takeScore() {
      scoreOne--;
      tfScoreOne.textProperty().setValue("Score: " + Integer.toString(scoreOne);
   }
}

public void btnScoreAddHandler(EventHandler<ActionEvent> handler) {
    btnOneAdd.setOnAction(handler);
}

public void btnPenaltyAddHandler(EventHandler<ActionEvent> handler) {
    btnOneAdd.setOnAction(handler);
}

Control:

public class SampleController {

   public ModuleSelectionController() {
      // Some contorller stuff again
      samplePaneObj.btnScoreAddHandler(btnScoreHandler);
      samplePaneObj.btnPenaltyAddHandler(btnScoreHandler);
   }

   private class btnScoreHandler implements EventHandler<ActionEvent> {
        public void handle(ActionEvent arg0) {
            samplePaneObj.giveScore();
        }
   }

   private class btnPenaltyHandler implements EventHandler<ActionEvent> {
        public void handle(ActionEvent arg0) {
            samplePaneObj.takeScore();
         }
   }

}

This is mostly pseudocode so forgive me if there are any errors but do you get the point? It seems very arbitrary to just be calling methods but without passing the TextField in the example its hard to not do everything without a method doing all the work.

But is that decoupled enough for MVC? I don't really wanna break encapsulation is the main issue so I can't make the controls public and operate on them directly in the controller.

Is this all just normal? I want to make sure I'm grasping it right.

N R
  • 37
  • 5
  • 1
    The "M" in "MVC" stands for "Model": you don't seem to have a model at all. There are a lot of variations of the MVC pattern (MVP, MVVM, etc), but the one thing in common in all of them is having a class, or classes, representing the data: I recommend the model being the place you start when building an app. See, e.g. https://stackoverflow.com/questions/32342864 – James_D May 07 '20 at 13:13

1 Answers1

1

There is too much that could be said about this here. I'd advise you to have a look at a JavaFX application framework and read its documentation. I learned a lot from it. E.g., have a look here: https://github.com/sialcasa/mvvmFX Don't make the mistake and try to derive some implementation patterns yourself from all the hello world examples out there on the internet. They all don't teach you how things should be done so that they scale well for real-world projects.

mipa
  • 10,369
  • 2
  • 16
  • 35