Java MVC – Using MVC in a Java Application

javajavafxmvcswing

I need to write a cross-platform GUI application to process (in multiple threads) and visualize fairly large quantities of data. Ideally the application should be relatively fast and look good.

The app's interface will consist of a table widget, a tree widget, and a custom figure-drawing widget. The user will be able to modify the data from any one of these widgets and the changes should be immediately reflected in the other widgets.

Naturally, I am planning to use MVC. However, I normally do all my GUI programming in C++/Qt, and have very limited exposure to Java. So I would really appreciate any advice on how to organize such an app in Java. In particular, should I use Swing or JavaFX? Which widgets would you pick for the job? Could you recommend any books/online tutorials that cover these aspects of the Java platform?

I will greatly appreciate any feedback. Thanks!

(this question was originally posted on Stack Overflow, but this site was suggested as a more appropriate place to ask it)

Best Answer

This is all very subjective as multiple different technologies will be able to serve your needs.

Like others, I'll probably just recommend the technology I'd personally prefer for such a project, which would be JavaFX.

Reasons I would recommend JavaFX are:

OK, I guess the above is a bit of a tangent, but I like to promote JavaFX ;-)

Anyway, to address the specific points in your question:

  • JavaFX is a cross-platform GUI framework (currently Mac/Windows/Linux).
  • JavaFX has inbuilt, high quality multiple threading support (the core framework itself is single-threaded as is just about every other GUI platform out there, but you can define your own tasks to be executed concurrently off the GUI thread so that the GUI thread remains responsive).
  • A well written JavaFX program should have no issues performing well enough to visualize fairly large quantities of data. Here is a sample JavaFX project which does that.
  • The ability to style the application via css, make use of visual effects and animation, allows you to work with designers to make your app look great or do it yourself if you have the skills.
  • JavaFX has a TableView and a TreeView you can use. There is also a combined TreeTable control which has been developed in preparation for the Java 8 release.
  • The custom figure drawing widget could use scene graph shapes or a direct draw canvas - it's really just a matter of coding style and preference.
  • JavaFX's properties and binding facilities coupled with it's event listener frameworks makes it trivial to modify data using one control and have the changes immediately reflected in other controls.
  • For MVC style development, you can write your model using plain java objects, define your view using the FXML markup language (created using the SceneBuilder graphical layout tool if desired) and define your control logic in Java (or another scripting language if you prefer), plus you should separate your style from your logic by using CSS.
  • As you have limited exposure to Java, the learning curve will be significant. There are excellent Java tutorials in addition to the JavaFX tutorials I linked to earlier which can help with this. JavaFX core code uses only facilities available in the JDK, so to use JavaFX you don't need to learn a lot of additional libraries and frameworks (as you would if you were learning JavaEE for instance). So those two tuotorial sites provide most information you need to get up to speed.
  • For app organization, if you have a complex application and need the backing of a complete, proven client application framework and don't mind doing a lot of extra learning, then you can embed JavaFX in the Eclipse RCP or NetBeans RCP, which, according to their pages could save you years of development time. I'd only advise looking at such platforms if your needs justify it though, because for small to medium size projects, it will probably be simpler to code the stuff up directly in JavaFX without the complex client platform frameworks such as Eclipse and NetBeans.
  • As to whether you should use Swing or JavaFX, I'm not really the most objective person to answer that. JavaFX definitely has shortcomings which would become apparent to you as you start to use it (as does Swing).
  • For online tutorials, I've linked to most relevant ones. There is a nice tutorial set for a complete app.
  • The books Pro JavaFX 2 and JavaFX 2.0 Introduction by Example are highly rated.
Related Topic