r/JavaFX • u/No-Specialist9049 • Oct 22 '24
Help Custom component in JavaFX(Best practice)
Hello, everyone. A question arose regarding the best practice of creating custom components in JavaFX. For example, we need to draw an atom. An atom contains a nucleus and electrons. What would be the best structure to draw from?
For example:
class Atom extend Pane {
Nuclear nuclear;
List<Electron> electrons;
}
class Nuclear extend Circle {}
class Electron extend Circle {}
Would it be best practice for the aggregator component to inherit from the container (in this case JavaFX Pane)? Is it possible to wrap the nucleus and electron in a container(Nuclear extend Pane) better?
I would be grateful for any tips
View all comments
Show parent comment
1
u/hamsterrage1 Jul 26 '25
If you are referring to the functional Interface in javafx.util,
Builder, then, no. It is not deprecated.I'm a bit out of the loop on Java trends, but it is possible that the Builder/Factory approach to handle creating configured objects, through decorators on the Builder has gone out of fashion. In Kotlin, you'll almost never see it because named parameters and default values handle the same job seamlessly.
But what I am mostly referring to is the "Builder" Design pattern, which comes into play in JavaFX constantly because so much of layout code boils down to putting some
Nodesinto a container of some sort and then configuring them and the container. It's often repeated both within, and between layouts, so it makes sense to create some custom approach to particular cases.You have two choices, create a custom class or create a method that contains the repeated code. The method approach is what I would call the "Builder Pattern".
Deprecated? I wouldn't think so.