2

I am creating a scrollpane which display sets of data which is fetched from a sqlite db and these data are display in such a way that they arranged in repeated set of ui like in recyclerview in android. Is there any way to achieve it because its not easy to position elements using javafx

Adarsh Jain
  • 165
  • 1
  • 2
  • 12

2 Answers2

2

There are a few classes that work with similar concepts in javafx:

The adapter is replaced by the cellFactory(s), position does not really have an equivalent, the cells do contain a index property though. There is a updateItem method in the Cell class that is responsible for updating the cell visuals.

Please use some tutorials to become familiar with those kind of controls, since this is beyond the scope of an answer.

ListCell works similar to TableView, but there is basically just a single column, and the cellFactory is a property of the ListView class itself and there is no cellValueFactory equivalent.

TreeView and TreeTableView work pretty similar to ListView and TableView respecively, the main difference being the support of a hierarchical data structure.

Graham
  • 7,431
  • 18
  • 59
  • 84
fabian
  • 80,457
  • 12
  • 86
  • 114
0

I'd implement my own ListCell, and then use it in a ListView. It would be something like:

public class RecyclerLikeCell extends ListCell<MyCustomBean> {

    @Override
    protected void updateItem(MyCustomBean item, boolean empty){
        super.updateItem(item, empty);

        //Create whatever you need to render. And then add:
        //I'm using a VBox as an example, you may use whatever you need.
        var myComponent = new VBox(component1, component2, component3);
        setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
        setGraphic(myComponent);
    }

    /**
     * Utility method to use as ListCellFactory.
     *
     * @return a Callback to use in ListView.
     */
    public static Callback<ListView<MyCustomBean>, ListCell<MyCustomBean>>
        forListView(){

        return i -> new RecyclerLikeCell();
    }

}

And then, in your controller initialization you can use:

listRecyclerView.setCellFactory(RecyclerLikeCell.forListView());

And it should be fine. I've used it to create a user menu like the one on SAP Business One GUI.

bichoFlyer
  • 178
  • 9