11

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.

Shreyas Dave
  • 3,815
  • 3
  • 28
  • 57

4 Answers4

23

You have to try this:-

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

its 100% working....

Shreyas Dave
  • 3,815
  • 3
  • 28
  • 57
Vikas Tiwari
  • 367
  • 1
  • 11
5

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 ;
    }
}
James_D
  • 201,275
  • 16
  • 291
  • 322
5

With fxml:

<ListView fx:id="foundContentList">
    <placeholder>
        <Label text="Nothing found" />
    </placeholder>
</ListView>
matepal297
  • 961
  • 1
  • 11
  • 19
-2

Not entirely sure but I don't think there is a setPlaceholder method(to set the default message when no content in table) for ListView.

The workaround that I use is to create an Object in the list that indicate "No content" and show that on the listview and also disable it.

For example:


ObservableList noContent= FXCollections.observableArrayList("No content found");
ListView listView = new ListView(noContent);
listView.setDisable(true);

francisOpt
  • 860
  • 1
  • 8
  • 15