The ControllerFX creates connection of the FXML to Scala code and underlying ModelFX for the application logic.
The ControllerFX creates connection of the FXML to Scala code and underlying ModelFX for the application logic.
Constructor argument names correspond to controls defined in FXML and the model. The constructor is used by ScalaFXML macro to automatically expose FXML controls in Scala code of the view class.
See more details in the org.scalafx.extras.mvcfx
documentation.
Example:
import org.scalafx.extras.mvcfx.ControllerFX import scalafx.Includes._ import scalafx.scene.control.{Button, Label} import scalafxml.core.macros.sfxml @sfxml class StopWatchController(minutesLabel: Label, secondsLabel: Label, fractionLabel: Label, startButton: Button, stopButton: Button, resetButton: Button, model: StopWatchModel) extends ControllerFX { ... startButton.disable <== model.running stopButton.disable <== !model.running resetButton.disable <== model.running ... }
MVCfx is the "root" class for creation of UI components using MVCfx pattern.
MVCfx is the "root" class for creation of UI components using MVCfx pattern. It instantiates and binds together the model, the controller, and the view (FXML).
The implementation of a class that extends MVCfx is very simple, it only needs instance of the model and information about location of the FXML resource. For example:
import org.scalafx.extras.mvcfx.MVCfx class StopWatch(val model: StopWatchModel = new StopWatchModel()) extends MVCfx("/org/scalafx/extras/mvcfx/stopwatch/StopWatch.fxml")
The implementation will include: * StopWatch extends MVCfx * StopWatchModel extends ModelFX * StopWatchController extends ControllerFX * StopWatch.fxml
The complete example in in demo module.
See more details on MVCfx see org.scalafx.extras.mvcfx
documentation.
Trait for for implementing component logic.
Trait for for implementing component logic. Is not aware how the UI structure is implemented. Contains references to parent and parentWindow to help display dialogs.
See more details in the org.scalafx.extras.mvcfx
documentation.
Package
mvcfx
helps in implementation of Model-View-Controller-like patters, we call it MVCfx. The pattern is build around use of views defined in FXML (the view), with binding to ScalaFX using ScalaFXML library.There are two cooperating classes
ControllerFX
for binding FXML to Scala code andModelFX
that contains logic for the component. An additional helperMVCfx
class is provided to instantiate theControllerFX
and the correspondingModelFX
.The structure of the UI component is defined in a standard JavaFX FXML file. The Scala side of the FXML is in a class
ControllerFX
. Part of theControllerFX
is automatically generated by ScalaFXML macro, the rest can be customized as needed, including binding of the UI to appropriate parts of the component logic represented by theModelFX
. Use of theMVCfx
is optional and primarily intended to simplify instantiation of theControllerFX
and theModelFX
.Below is an example using the classes in
mvcfx
. The code implements a simple Stop Watch. The complete code is in demos part of the ScalaFX Extras project.Note the recommended naming convention used: * StopWatch extends MVCfx * StopWatchModel extends ModelFX * StopWatchController extends ControllerFX * StopWatch.fxml the FXVM declaration of the view
First we have the FXML definitions representing the structure of the user interface (
org/scalafx/extras/mvcfx/stopwatch/StopWatch.fxml
)The
ControllerFX
creates connection of the FXML to Scala code and underlyingModelFX
for the application logic. Note the@sfxml
annotation and the constructor arguments corresponding to controls defined in FXML, likeminutesLabel
. The last argument to the constructor is theModelFX
. (org.scalafx.extras.mvcfx.stopwatch.StopWatchController
)The
ModelFX
implements the logic for the operation of the Stop Watch. Notice that there are no direct references to UI controls. The connection to the UI is through the properties (likeminutes
). TheModelFX
is not aware how theControllerFX
is implemented. (org.scalafx.extras.mvcfx.stopwatch.StopWatchModel
)The
MVCfx
implementation is very simple, it only needs instance of the model and information about location of the FXML resource. (org.scalafx.extras.mvcfx.stopwatch.StopWatch
)The
MVCfx
can be easily used in an Application class to create UI. (org.scalafx.extras.mvcfx.stopwatch.StopWatchApp
)