Understanding JavaFX MVC Class Methods

Asked 2 years ago, Updated 2 years ago, 185 views

I'm making a simple app with JavaFX to practice MVC, but I can't make it well.
Especially, I am not good at communicating View input to the controller.

Below is the source I am making now, but I use the method of settingOnAction in View and handing it over to the controller with id.However, since View does not have an instance of the controller, the method received by the controller needs to be static, and if you call the Model method from there, you will only be able to use static.

The workaround is

  • Give the controller instance to View...MVC-like...?
  • Create an instance of a Model with the Controller constructor...I'm reading it in static, so it's clogged up

I thought about it, but when I thought about it carefully, I felt that there was a problem.
In the first place, the method of handing it over to the controller itself seems suspicious.
Could someone give me some advice?
Thank you for your cooperation.

By the way, I have no plans to use fxml this time.
Also, the following code is long, so import is omitted.

MainController

public class MainController extensions Application {
    public static Stage stage;
    @ Override
    public void start (Stage primaryStage) {
        // Initial Configuration
        primaryStage.setWidth (1000);
        primaryStage.setHeight(500);
        primaryStage.setTitle("Title");
        primaryStage.show();

        stage=primaryStage;
        new MainView (primaryStage); // Display
    }

    // Button processing
    public static void inButton (Evente, String id) {
        switch(id){
        case "0" :// My Page
            break;
        case "1" :// Calculation page
            new CalController (stage);
            break;
     }

    public static void main(String[]args) {
      launch(args);
    }
}

MainView

public class MainView {
    EcMainView (Stage stage) {
        VBox root=newVBox();
        Button [ ] button = new Button [2];
        button[0] = new Button("My Page");
        button[1] = new Button("calculation");
        for(inti=0;i<2;i++){
            int tmpI = i;
            button [tmpI].setId("+tmpI);
            button [tmpI].setOnAction(e->EcMainController.inButton(e, button [tmpI].getId()));
        }
        root.getChildren().addAll(button);
        stage.setScene(newScene(root));
    }
}

CalController

public class CalController {
    private Stage stage;
    private CalView calView;
    private CalModel calModel;
    CalController (Stage stage) {
        This.stage=stage;
        calView = new CalView (stage); // View
        calModel = new CalModel();
    }
    public static void inButton (Evente, String id) {
        switch(id){
        case "0":// file selection
            /*----- This is especially troubling ----*/
            CalModel.getFileList();
            break;
        case "1" :// Edit
            break;
        }
    }
}

CalView

public class CalView {
    CalView (Stage stage) {
        calViewMain (stage);
    }
    void calViewMain (Stage stage) {
        VBox root=newVBox();
        Button [ ] button = new Button [2];
        button[0] = new Button("Read File";
        button[1] = new Button("Edit");
        // Common initial configuration of buttons
        for(inti=0;i<button.length;i++){
            int tmpI = i;
            button[i].setId(i+"");
            button[i].setOnAction(e->CalController.inButton(e, button[tmpI].getId()));
        }
        HBox hBox = new HBox();
        hBox.getChildren().addAll(button);
        // View
        root.getChildren().addAll(hBox);
        stage.setScene(newScene(root));
    }
}

CalModel

public class CalModel{
    static String filePath="data";
    public static String [ ] getFileList() {
        String [ ] list;
        readText();
        // Listing
        return list;
    }
    public static String readText() {
        String text="";
            // Load file
        return text;
    }
}

java mvc javafx

2022-09-30 21:20

1 Answers

I solved myself by referring to mvc in java swing.
Specifically

  • View
    Have an instance of Model
    Methods available to retrieve button systems
  • Controller
    Call the View button retrieval method to register as a listener (setOnAction)
    View,Model creates an instance and associates a model instance with View

That's what I did.

Key Reference Sites
http://d.hatena.ne.jp/dounanda/20110505/1304586226

Controller

public class ExamCalC{
    ExamCalV calV;
    ExamCalMcalM;
    ExamCalC (Stage stage) {
        calV = new ExamCalV(stage);
        calM = new ExamCalM();
        calV.bindModel (calM);
        // Behavior with buttons
        calV.getButton(0).setOnAction(e->button1());// Button1
        calV.getButton(1).setOnAction(e->button2());// Button2
    }
    void button1(){}
    void button2(){}
}

View

public class ExamCalV{
    ExamCalMcalM;
    private Button [ ] button;
    private BorderPane root;
    private VBox vBoxScore;
    private AnchorPane topBar;
    private AnchorPane bottomBar;

    ExamCalV (Stage stage) {
        root = new BorderPane();
        // button
        button = new Button [4];
        button[0] = new Button("1");
        button[1] = new Button("2");
        button[2] = new Button("3");
        button[3] = new Button("4");
        // Common initial configuration of buttons
        for(inti=0;i<button.length;i++){
            button[i].setId(i+"");
        }

        // Top button
        AnchorPane.setLeftAnchor (button[0], 10.0);
        AnchorPane.setRightAnchor (button[1], 10.0);
        topBar = new AnchorPane();
        topBar.getChildren().addAll(button[0], button[1]);

        // Center Layout
        vBoxScore=newVBox();

        // Bottom button
        AnchorPane.setLeftAnchor (button[2], 10.0);
        AnchorPane.setRightAnchor (button[3], 10.0);
        bottomBar = new AnchorPane();
        bottomBar.getChildren().addAll(button[2], button[3]);
        // View
        root.setTop(topBar);
        root.setCenter(vBoxScore);
        root.setBottom(bottomBar);
        stage.setScene(newScene(root));
    }
    Button getButton(intid){
        for(inti=0;i<button.length;i++){
                if(button[i].getId().equals(id+""){
                return button [i];
            }
        }
        return null;
    }
    // Configuring Reference Model
    void bindModel (ExamCalMcalM) {
        this.calM = calM;
    }
}


2022-09-30 21:20

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.