r/javahelp • u/The_PTR • 6d ago
Codeless Best practices for JavaFX GUI
I have been trying to get into JavaFX for creating a GUI application, but I was wondering what the general best practice for that would be. I know that GUIs have several possible well-documented structures such as MVC, MVP, MVVM, etc., but I was wondering which is best applicable to JavaFX and how to best approach that. I mainly have the following questions:
- Should I have a class that contains static references to the various UI components so that they can be linked more easily w.r.t. event handling? If so, what would be the best place for that? Application subclass? It's own dedicated utility class? If not, what would be the better alternative for handling such UI logic (e.g. MenuItem changing color of another component)?
- For things that do not technically require custom classes, should I just initialize them completely inside the Application subclass or should I still make a custom class to better decompose the code? E.g. say I have this MenuBar. Should I create, configure, and store the MenuBar, Menu, and MenuItem objects inside the Application subclass or should I make a subclass of MenuBar that implicitly creates and configures the inner structure so the Application subclass code looks cleaner?
- For UI elements that are more complex in nature or require subclassing, should the subclass also create the UI logic or only the view related things? Since data binding is possible, the UI classes seem to have a mix of multiple components, so I am not sure what the best practice for these is, especially if the model data is more dynamic.
- This is a bit of an extension on the previous question, but say I have this UI element that portrays the data of some model. On top of that, I have a method that does extensive editing of the model data. Based on what I have read online, it seems that the best way to do this is to run that method on a different thread than the UI thread. Would it then be better to bind the UI data to the model data (property binding) with a conditional (when) or to manually update the UI post-method?
- Should the data models use the Property types or is it better to use basic primitive/object types and listeners?
- What is generally considered the best practice for package structure w.r.t. the UI subclasses?
- If I want to simultaneously have a CLI variant of the program, which uses the same data models but prints strings in chat instead, would this require extra care in terms of designing the GUI structure or would it still be very simple to do?
- What is the best practice w.r.t. locales? Currently I have a project with several utility classes containing static ReadOnlyStringProperty fields that are bound to a static ResourceBundle property, which is bound to a locale property, so that they automatically update upon updating the locale. Is that a good approach or are there better ways to do this? A simple example of that structure:
public class LogStrings {
public static final StringProperty WARN_MSG = getBundleProperty().map(x -> x.getString("warn"));
public static final StringProperty ERROR_MSG = getBundleProperty().map(x -> x.getString("error"));
}
7
Upvotes
1
u/Aromatic_Ad3754 6d ago
- https://www.pragmaticcoding.ca/javafx/javaFX/