Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC with javaFX

Tags:

I'm struggling with the MVC concept using javaFX. I am building an javaFX application using fxml files.

Each fxml file has a controller assigned, but I don't think that this controller is one as the MVC pattern states. I think of it like some sort of ViewController, which holds references to fxml objects (buttons, panes etc.).

My problem is: Where exactly is the difference between this "ViewController" and the real "Controller". What object should do what things? Where do I set e.g. actionListeners?

like image 699
priojewo Avatar asked Apr 20 '14 21:04

priojewo


People also ask

Is FXML MVC?

In JavaFX, the FXML file used to generate the View also provides a critical role in MVC loading. The FXML file should contain a document reference to the Controller object. This facilitates loading of Controller and binding it with the View at initialization.

Is MVC a design pattern?

The Model-View-Controller (MVC) is an architectural pattern which separates an application into three main groups of components: Models, Views, and Controllers. MVC is abbreviated as Model View Controller is a design pattern created for developing applications specifically web applications.

Can JavaFX be used with swing?

JavaFX SDK provides the JFXPanel class, which is located in the javafx. embed. swing package and enables you to embed JavaFX content into Swing applications.

Can JavaFX be used for games?

Games can be done by animation packages in JavaFX. JavaFX provides rich GUI for gaming applications. JavaFX also provides beautiful graphics, this graphics are providing in JavaFX by using Canvas. Most of the JavaFX games has time based games so we can add time related operations with AnimationTimer class.


1 Answers

Thoughts on MVC

MVC is a pretty loosely defined pattern which is open to (often somewhat vague) interpretations of what each of the things in MVC stand for (especially the controller). There is a great discussion of the MVC architecture with respect to GUI toolkits by Martin Fowler.

On Design Patterns and FXML

JavaFX core FXML based processing is built to be more of a toolkit rather than a complete development framework. The idea being that other frameworks could be layered on top of JavaFX and FXML and the underlying JavaFX/FXML implementations and the controllers for them would not push any kind of agenda or architectural constraints on the higher level frameworks.

As a result, there is a deliberately loose analogy and mapping of core FXML based processing and their controllers to an MVC architecture.

See JavaFX and MVP – a smörgåsbord of design patterns for further discussion.

Consider using a higher-level framework

You might benefit from adopting an "opinionated" JavaFX framework such as afterburner.fx, which utilizes controllers and FXML but provides a bit more of a rigid structured framework. Afterburner.fx follows a Model View Presenter (MVP) model. Though Afterburner.fx provides more functionality and structure than plain core JavaFX+FXML, it does so in a minimal way by adding few extra classes and APIs that you need to learn to use it.

like image 184
jewelsea Avatar answered Sep 28 '22 15:09

jewelsea