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)
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:
- In-built properties and binding make it easy to wire up parts of your GUI for automatic updates.
- Quality graphical design tool.
- Separation of UI component layout from control logic using FXML.
- High quality, hardware accelerated, platform independent rendering architecture.
- Wide variety of supported platforms and systems.
- Support for touch interaction.
- Separation of style from code using CSS.
- SceneGraph based retained rendering model rather than a direct draw model.
- Quality documentation and support forums plus help available via StackOverflow and a web blogging community.
- Wide variety of quality pre-built controls.
- Media support.
- Modern, performant HTML5 engine.
- Embeddable in Swing applications if needed, so you can still make use of Swing based frameworks.
- Embeddable in SWT applications if needed, so you can still make us of SWT based frameworks.
- Ability to support immediate mode rendering if required.
- Effects framework.
- Animation framework.
- Graphing framework.
- Open source development.
- Excellent sample program.
- Public issue tracker and responsive developers, doing active, on-going development so that new features and bugs get addressed in a timely way.
- Backing of a major company prepared to invest considerable resources to make the project successful.
- Deployable via native platform installers and executables.
- Able to utilize all of the features of the massive JDK and java based libraries and projects.
- Can be used from a variety of programming languages, such as Scala, Groovy and Ruby.
- Nice interactive development and debugging utilities as well as the ability to use a standard Java debugger to debug your program.
- Quality layout managers.
- JavaFX has been designed to take advantage of Java 8 lambdas.
- JavaFX applications are deployable to commercial app stores.
- JavaFX can be programmed using the best IDEs in the world.
- Strong concurrency utilities.
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.
4
Go with a Model-view-presenter pattern instead. You can view a good MVP example in Swing here via the mvp4j project.
While not Swing, I’d also check out the MVP articles over at the GWT Google Developers site for further insight on this pattern and how to apply it in Java; the same design principals stand regardless of the framework and GWT is very similar to Swing.
A quick breakdown of how MVP works:
- View: A class that strictly contains your GUI components. No awareness of logic or model, not even event handlers. This makes views very dumb, yet very disposable and changeable.
- Presenter: A class that handles your application logic. A presenter will bind to one (or more) view classes and handle all the necessary application logic and model control.
- Model: Your data model objects. It’s best to keep these as POJO-like as possible (plain old java objects).
When implemented correctly, MVP will make your application very decoupled and allow you to make modifications without disturbing other areas of your application.
Edit: Based on your decision to use JavaFX, I’d recommend checking out the following articles
- Effective JavaFX Architecture
- JavaFX And MVP – A Smörgåsbord Of
Design Patterns - JavaFX MVP Example
2
The widgets that you require can be found in both Swing or SWT. The documentation contains examples of components (Swing) or widgets (SWT) so it will be fairly easy to identify them.
Swing is GUI library included in JDK and built from scratch. SWT is an external one and the components are based on native ones.
As for MVC, they both have support for it. In Swing you have a Model for each component that practically provides the underlying data. The component itself is both the View and the Controller.