JavaFx: default message for Empty ListView

When there is no record in any table, it displays the message “No content in the table”, which by default is the TableView function in JavaFx.

So here is my question: is this possible with ListView in JavaFx? For example, if there is no element in any ListView element, it will display the same message as TableView, instead of empty / empty fields.

+8
java listview javafx javafx-2 javafx-8
source share
4 answers

You should try the following: -

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

its 100% working ....

+17
source share

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

In earlier versions you need to somehow roll. It's a bit hacky: it wraps a ListView on the stack stack, with a white rectangle and a placeholder displayed at the top of the list. The placeholder and rectangle have a visible visibility property, so they are visible only if the list is empty.

There may be simpler ways that I don't see 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 ; } } 
+4
source share

Using fxml:

 <ListView fx:id="foundContentList"> <placeholder> <Label text="Nothing found" /> </placeholder> </ListView> 
+1
source share

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

The workaround that I use is to create an object in the list that indicates “No content”, and show it in the list, and also disable it.

For example:

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

All Articles