Integration of DukeScript Models with UI defined in FXML and rendered in JavaFX.
Java
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
src
.gitignore
README.md
pom.xml

README.md

Use FXML to define your UI and use @Model to define your MVVM logic. To use the system in your JavaFX application, create standard Scene.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns:fx="http://javafx.com/fxml/1" >
    <children>
        <Button layoutX="126" layoutY="90" text="Click Me!" onAction="$controller.handleButtonAction"  />
        <Label layoutX="126" layoutY="120" minHeight="16" minWidth="69" fx:id="label" text="${controller.testString}" />
        <ListView layoutX="126" layoutY="160" minHeight="60" minWidth="100" items = "${controller.testList}">
        </ListView>
    </children>
</AnchorPane>

The FXML is referencing properties of your controller class, which can be easily defined using @Model annotation:

@Model(className = "ViewModel", properties = {
    @Property(name = "testString", type = String.class),
    @Property(name = "testList", type = String.class, array = true)
})
public class SimpleController {
    @Function
    static void handleButtonAction(ViewModel vm) {
        vm.setTestString("Hello World!");
        vm.getTestList().add("val4");
    }
}

Such model carries two properties and a function handleButtonAction which acts on the model and gets invoked by the button defined in the FXML file once it is clicked. At the end, one just instantiates the whole scene and binds it together with the controller:

FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("SimpleScene.fxml"));
viewModel = new ViewModel("Hallo Hallo", "val1", "val2", "val3");
fxmlLoader.setController(Models.toRaw(viewModel));
Parent rootNode = fxmlLoader.load();

Your model view view model completely separated from the JavaFX UI definition has just become reality!