< Java FX

JavaFX MVC

Szerző: Sallai András

Copyright © Sallai András, 2021

Licenc: CC Attribution-Share Alike 4.0 International

Web: https://szit.hu

FXML

app01/
`-src/
|-controllers/
| `-MainController.java
|-fxml/
| `-MainView.fxml
`models/
`-MainModel.java

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); } }
<?xml version="1.0" encoding="UTF-8"?> <?import javafx.scene.control.*?> <?import java.lang.*?> <?import javafx.scene.layout.*?> <VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controllers.MainController"> <children> <Label text="Label" /> <Button mnemonicParsing="false" onAction="#onActionButton" text="Button" /> </children> </VBox>
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; } }