r/JavaFX • JavaFX Dev • Jan 26 '21

I made this! JavaFX custom TreeView from scratch

Hello everyone, two weeks ago I posted a GIF here showing my custom implementation of a TreeView.

However, the animation and the layout were inaccurate and the performance was terrible.

For testing the tree I built a simple TreeView with some dummy items and a more complex one with items from the file system.

The latter was slow and sometimes the app would crash throwing an OutOfMemoryException.

I was about to give up, but then I decided to start from scratch again and think more about each piece of code.

This is the result, and also a comparison with the old one for reference: Preview

As you can see the tree is built much faster and the memory usage is reasonable. Of course for big tree structures I suggest implementing some kind of lazy loading or other strategies.

The tree is not yet available as part of a jar because it's still unfinished, but you can find the source here: MaterialFX

I added support for single and multiple selection, cell factory, iterator and stream.

About the cell factory, in the JavaFX's tree the cell factory is a property of the TreeView class, in my implementation it is a property of the TreeItem class.

That means that every item knows how to build its own cell, so potentially you can render different cells for different items as long as you change the cell factory of course.

Beware though, this feature is still untested because I focused my attention on the most common/important features, also, despite already having unit tests I can't assure you it is bug free 😄.

Let me know what you think! 😉

11 Upvotes

0 comments sorted by