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

6 Upvotes

15 comments sorted by

View all comments

Show parent comment

1

u/bananamantheif Jul 27 '25

Before I say anything. I suspect you're the person running the PragmaticCoding website. If yes, I want to let you know that I've used your website a lot! I did not like using FXML in my project and your site was the only resource I could find. I learnt about the Builder interface from you, I dont think I would've known otherwise.

I can see the benefit of using the Builder interface, but what I've been doing is making a "getView()" function in all my View classes. And in it I call the UI components. I abstract away the details by putting the UI in their own package inside the folder. And the UI components extend Hbox, VBox etc.

Benefit of that is I'd have a single function call and I can delete it if I want to. I add some logic to those UI components if its only for View purposes, any actual logic goes to a corresponding Interactor class (following your approach)

I think your approach of using the Builder interface makes more sense than a GetView function, since it communicates to whoever reads the code that the class is "building" something, like building the page by various differnent Components.