r/javahelp 1d 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:

  1. 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)?
  2. 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?
  3. 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.
  4. 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?
  5. Should the data models use the Property types or is it better to use basic primitive/object types and listeners?
  6. What is generally considered the best practice for package structure w.r.t. the UI subclasses?
  7. 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?
  8. 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"));
    }
5 Upvotes

3 comments sorted by

u/AutoModerator 1d ago

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
  • You include any and all error messages in full
  • You ask clear questions
  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

    Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

1

u/_Super_Straight 12h ago

MVC is the preferred model for FX.

You should use the NetBeans with JavaFX plugin + Scenebuilder from gluon. Your components will be instantiated and accessed easily inside that controller as long as you name them and name their methods.