Overview
JList is a wrapper around JavaFX ListView that provides:
- Built-in pagination controls
- Infinite scroll capability
- Custom cell factory support with automatic styling
- Easy item management
Constructor
Creates a new generic list for type T.
Basic Usage
JList<User> list = new JList<>();
list.setCellFactory(param -> new ListCell<User>() {
@Override
protected void updateItem(User user, boolean empty) {
super.updateItem(user, empty);
if (empty || user == null) {
setText(null);
setGraphic(null);
} else {
setText(user.getName());
}
}
});
list.setItems(userData);
Cell Factory
Sets the cell factory for rendering list items. Automatically adds the “j-list-card-cell” style class to each cell.
value (Callback<ListView<T>, ListCell<T>>): Factory function that creates ListCell instances
list.setCellFactory(param -> new ListCell<Product>() {
@Override
protected void updateItem(Product product, boolean empty) {
super.updateItem(product, empty);
if (empty || product == null) {
setGraphic(null);
} else {
JCard card = new JCard();
card.setTitle(product.getName());
card.setDescription(product.getDescription());
setGraphic(card);
}
}
});
Data Management
Sets the list’s data source.
items (ObservableList<T>): The observable list of items to display
Adds a single item to the list.
item (T): The item to add
Shows or hides the pagination controls.
visible (boolean): Whether pagination should be visible
Returns the underlying JPagination component for advanced control.
Infinite Scroll
setOnScrollBottom(action)
Enables infinite scroll and sets a callback that fires when the user scrolls to 90% of the list.
action (Runnable): Callback to execute when scroll reaches near bottom
list.setOnScrollBottom(() -> {
// Load more items
loadMoreItems();
});
Access to Underlying Components
Returns the underlying JavaFX ListView for advanced customization.
Example: Card List
JList<Article> articleList = new JList<>();
articleList.setCellFactory(param -> new ListCell<Article>() {
@Override
protected void updateItem(Article article, boolean empty) {
super.updateItem(article, empty);
if (empty || article == null) {
setGraphic(null);
} else {
VBox card = new VBox(8);
card.getStyleClass().add("article-card");
JLabel title = new JLabel(article.getTitle());
title.withStyle("text-lg", "font-bold");
JParagraph excerpt = new JParagraph(article.getExcerpt());
excerpt.withStyle("text-muted");
JButton readMore = new JButton("Read More");
readMore.setVariant("link");
card.getChildren().addAll(title, excerpt, readMore);
setGraphic(card);
}
}
});
articleList.setItems(articles);
articleList.setOnScrollBottom(() -> loadMoreArticles());