JavaFx :Default Message for Empty ListView


Question

When there is no record in any table it shows a message 'No content in table', which is by default functionality of TableView in JavaFx.

So here my question is, does the same can be possible with ListView in JavaFx ? Like, if there is no item in any ListView then it will show a message same as TableView, instead of a blank/empty fields.

1
8
12/10/2013 3:42:00 AM

Accepted Answer

You have to try this:-

listView.setPlaceholder(new Label("No Content In List"));

its 100% working....

17
12/10/2013 3:59:55 AM

JavaFX8 has a setPlaceholder(...) method for ListView.

In earlier versions, you need to roll your own somehow. This is a bit of a hack: it wraps the ListView in a stack pane, with a white rectangle and the placeholder displayed over the top of the list view. The placeholder and rectangle have their visible property bound, so they are only visible if the list is empty.

There may be easier ways that I'm not seeing right away...

import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class ListViewPlaceholderTest extends Application {

    @Override
    public void start(Stage primaryStage) {
        final ListView<String> listView = new ListView<>();
        final IntegerProperty counter = new SimpleIntegerProperty();
        final Button addButton = new Button("Add item");
        addButton.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                counter.set(counter.get()+1);
                listView.getItems().add("Item "+counter.get());
            }
        });
        final Button removeButton = new Button("Remove");
        removeButton.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                listView.getItems().remove(listView.getSelectionModel().getSelectedIndex());
            }
        });
        removeButton.disableProperty().bind(Bindings.equal(listView.getSelectionModel().selectedIndexProperty(), -1));
        final HBox buttons = new HBox(5);
        buttons.setPadding(new Insets(10));
        buttons.getChildren().addAll(addButton, removeButton);

        final BorderPane root = new BorderPane();
        root.setCenter(createPlaceholderForListView(listView, new Label("No content in List")));
        root.setBottom(buttons);

        final Scene scene = new Scene(root, 600, 400);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }

    private Node createPlaceholderForListView(ListView<?> listView, Node placeholder) {
        final StackPane pane = new StackPane();
        final Rectangle rect = new Rectangle(0, 0, Color.WHITE);
        rect.widthProperty().bind(listView.widthProperty());
        rect.heightProperty().bind(listView.heightProperty());
        pane.getChildren().addAll(listView, rect, placeholder);

        placeholder.visibleProperty().bind(Bindings.isEmpty(listView.getItems()));
        rect.visibleProperty().bind(placeholder.visibleProperty());
        rect.setMouseTransparent(true);

        return pane ;
    }
}

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon