[[oktatas:programozás:java:java_fx|< Java FX]]
====== JavaFX MVC ======
* **Szerző:** Sallai András
* Copyright (c) Sallai András, 2021
* Licenc: [[https://creativecommons.org/licenses/by-sa/4.0/|CC Attribution-Share Alike 4.0 International]]
* Web: https://szit.hu
===== FXML =====
app01/
`-src/
|-controllers/
| `-MainController.java
|-fxml/
| `-MainView.fxml
`models/
`-MainModel.java
{{:oktatas:programozas:java:java_fx:fxml.png?400|}}
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class App extends Application {
public void start(Stage primaryStage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource(
"fxml/MainView.fxml"));
Scene scene = new Scene(root, 300, 250);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) throws Exception {
launch(args);
}
}
package controllers;
import javafx.event.Event;
import javafx.fxml.FXML;
import models.MainModel;
public class MainController {
MainModel mainModel = new MainModel();
@FXML
private void onActionButton(Event event) {
System.out.println("Ez is Kattintás törtétn");
System.out.println(mainModel.getNum());
event.consume();
}
}
package models;
public class MainModel {
public int getNum() {
return 15;
}
}
===== Csak Java =====
app01/
`-src/
|-controllers/
| `-MainController.java
|-models/
| `-MainModel.java
`views/
`-MainView.java
import controllers.MainController;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import models.MainModel;
import views.MainView;
public class App extends Application {
public void start(Stage primaryStage) {
MainView mainWindow = new MainView();
MainModel mainModel = new MainModel();
new MainController(mainWindow, mainModel);
Scene scene = new Scene(mainWindow, 300, 250);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) throws Exception {
launch(args);
}
}
package views;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
public class MainView extends VBox {
public Button button;
public MainView() {
Text text = new Text("Helló Világ");
button = new Button("Mehet");
this.getChildren().add(text);
this.getChildren().add(button);
}
}
package controllers;
import models.MainModel;
import views.MainView;
public class MainController {
MainView mainWindow;
MainModel mainModel;
public MainController(MainView mainWindow, MainModel mainModel) {
this.mainWindow = mainWindow;
this.mainModel = mainModel;
eventsHandler();
}
public void eventsHandler() {
this.mainWindow.button.setOnAction( event -> {
System.out.println("Kattintás törtétn");
System.out.println(mainModel.getNum());
});
}
}
package models;
public class MainModel {
public int getNum() {
return 15;
}
}
===== Egyéb =====
* https://edencoding.com/mvc-in-javafx/ (2021)