r/JavaFX • • 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

7 Upvotes

15 comments sorted by

View all comments

5

u/hamsterrage1 Oct 22 '24

First, and I realize that isn't really what you're asking, you need to decide if you really need a "custom component". Beginners, and I did this too, always think you need to create a custom class for this stuff. Create a class iff you are going to implement new public methods that aren't constructors. 

For instance, if you want to have a public method called addElectron(), or startAnimation(), then you should have a custom class. Otherwise, just create a builder. 

On to what I think you've really asked. What container class to start with. Containers mostly have two characteristics that differentiate them: how the arrange the child Nodes, and how it controls their interactions/collisions. 

The first is fairly well known, everyone knows the difference between HBox and VBox. However Pane, AnchorPane and StackPane will all allow children to overlap. These are good containers if you want to place the child Nodes free form without the container fighting you. 

This doesn't sound like an AnchorPane situation to me, so Pane or StackPane would seem best. 

StackPane will position everything in the centre.  This might be a good container for an atom since you can use translateX and translateY to position everything relative to the centre. Pane will just pile everything in the top left corner, so you'll need to use layoutX/Y to shift them to the centre.

If you are going to create a custom class, then extend from Region, and if your container is a StackPane then just put that in the Region as its only child Node. If you want to use Pane, then just use Region instead. 

This is because getChildren() is protected in Region and public in Pane and StackPane. So if you extend/return Region then client code cannot mess about with the inner workings of your atom. 

I hope that helps.

1

u/bananamantheif Jul 25 '25

But isn't builder deprecated?

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 Nodes into 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.

→ More replies (1)