I would like to add a list of options to a JavaFX menu. The order of the options should be modified while the menu is showing (I will use fuzzy matching but the method for that is irrelevant for my issue). I can mimic such a behavior with CustomMenuItems that contain TextField and ListView, respectively:
import javafx.application.Application;
import javafx.application.Platform;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.CustomMenuItem;
import javafx.scene.control.ListView;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuButton;
import javafx.scene.control.TextField;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class MWE extends Application {
#Override
public void start(Stage primaryStage) {
final Menu menu = new Menu("MENU");
final List<String> options = Arrays.asList(
"AbC",
"dfjksdljf",
"skdlfj",
"stackoverflow");
final StringProperty currentSelection = new SimpleStringProperty(null);
final TextField fuzzySearchField = new TextField(null);
final CustomMenuItem fuzzySearchItem = new CustomMenuItem(fuzzySearchField, false);
// TODO unfortunately we seem to have to grab focus like this!
fuzzySearchField.addEventFilter(MouseEvent.MOUSE_MOVED, e->{fuzzySearchField.requestFocus(); fuzzySearchField.selectEnd();});
final ObservableList<String> currentMatches = FXCollections.observableArrayList();
// just some dummy matching here
fuzzySearchField.textProperty().addListener((obs, oldv, newv) -> currentMatches.setAll(options.stream().filter(s -> s.toLowerCase().contains(newv)).collect(Collectors.toList())));
final ListView<String> lv = new ListView<>(currentMatches);
lv.addEventFilter(MouseEvent.MOUSE_MOVED, e -> lv.requestFocus());
final CustomMenuItem lvItem = new CustomMenuItem(lv, false);
menu.getItems().setAll(fuzzySearchItem, lvItem);
fuzzySearchField.textProperty().addListener((obs, oldv, newv) -> currentSelection.setValue(currentMatches.size() > 0 ? currentMatches.get(0) : null));
fuzzySearchField.setText("");
menu.setOnShown(e -> fuzzySearchField.requestFocus());
final MenuButton button = new MenuButton("menu");
button.getItems().setAll(menu);
Platform.runLater(() -> {
final Scene scene = new Scene(button);
primaryStage.setScene(scene);
primaryStage.show();
});
}
}
However having a ListView inside a menu structure feels strange. That's why I tried to use MenuItems instead of the ListView:
import javafx.application.Application;
import javafx.application.Platform;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.CustomMenuItem;
import javafx.scene.control.ListView;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuButton;
import javafx.scene.control.MenuItem;
import javafx.scene.control.TextField;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class MWE2 extends Application {
#Override
public void start(Stage primaryStage) {
final Menu menu = new Menu("MENU");
final List<String> options = Arrays.asList(
"AbC",
"dfjksdljf",
"skdlfj",
"stackoverflow");
final StringProperty currentSelection = new SimpleStringProperty(null);
final TextField fuzzySearchField = new TextField(null);
final CustomMenuItem fuzzySearchItem = new CustomMenuItem(fuzzySearchField, false);
// TODO unfortunately we seem to have to grab focus like this!
fuzzySearchField.addEventFilter(MouseEvent.MOUSE_MOVED, e->{fuzzySearchField.requestFocus(); fuzzySearchField.selectEnd();});
final ObservableList<String> currentMatches = FXCollections.observableArrayList();
// just some dummy matching here
fuzzySearchField.textProperty().addListener((obs, oldv, newv) -> currentMatches.setAll(options.stream().filter(s -> s.toLowerCase().contains(newv)).collect(Collectors.toList())));
currentMatches.addListener((ListChangeListener<String>)change -> {
List<MenuItem> items = new ArrayList<>();
items.add(fuzzySearchItem);
currentMatches.stream().map(MenuItem::new).forEach(items::add);
System.out.println("Updating menu items!");
menu.getItems().setAll(items);
});
fuzzySearchField.textProperty().addListener((obs, oldv, newv) -> currentSelection.setValue(currentMatches.size() > 0 ? currentMatches.get(0) : null));
fuzzySearchField.setText("");
menu.setOnShown(e -> fuzzySearchField.requestFocus());
final MenuButton button = new MenuButton("menu");
button.getItems().setAll(menu);
Platform.runLater(() -> {
final Scene scene = new Scene(button);
primaryStage.setScene(scene);
primaryStage.show();
});
}
}
In that example, the menu does not get updated while showing, even though I can see "Updating menu items!" printed to the console, so the items of menu are being updated. The menu on the screen, however, does not change.
Is there a way of requesting a repaint of the menu?
Related questions:
JavaFX: Update menu sub-items everytime menu shown The solution seems incorrect, Menu does not seem to have a method setOnMouseEntered
Adding dynamic entries to Menu in JavaFX does not update the menu while it is showing (if I understand correctly)
I followed the suggestions by #Enigo and #Jesse_mw and used a custom node. Instead of a ListView, I decided to go with a VBox that contains only Labels because I only need basic functionality and do not want to have additional confusing handlers or highlighting. Also note that #kleopatra pointed out, that dynamic updates of items works out of the box for ContextMenu (and a potential bug in Menu), which is not an adequate choice for my use-case, unfortunately.
Here is my minimum working example code:
import javafx.application.Application;
import javafx.application.Platform;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.CustomMenuItem;
import javafx.scene.control.Label;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuButton;
import javafx.scene.control.TextField;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.CornerRadii;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class MWE extends Application {
private static Label label(final String text) {
final Label label = new Label(text);
label.addEventFilter(MouseEvent.MOUSE_MOVED, e -> label.requestFocus());
label.setMaxWidth(200);
final Background background = label.getBackground();
label.setOnMouseEntered(e -> label.setBackground(new Background(new BackgroundFill(Color.GRAY.brighter(), CornerRadii.EMPTY, Insets.EMPTY))));
label.setOnMouseExited(e -> label.setBackground(background));
// Do something on mouse press; in real world scenario, also hide menu
label.setOnMousePressed(e -> {
if (e.isPrimaryButtonDown()) {
System.out.println(label.getText());
e.consume();
}
});
return label;
}
#Override
public void start(Stage primaryStage) {
final Menu menu = new Menu("MENU");
final List<String> options = Arrays.asList(
"AbC",
"dfjksdljf",
"skdlfj",
"stackoverflow","ssldkfjsdaf", "sjsdlf", "apple juice", "banana", "mango", "sdlfkjasdlfjsadlfj", "lkjsdflsdfj",
"stackoverflow","ssldkfjsdaf", "sjsdlf", "apple juice", "banana", "mango", "sdlfkjasdlfjsadlfj", "lkjsdflsdfj",
"stackoverflowstackoverflowstackoverflowstackoverflowstackoverflowstackoverflow","ssldkfjsdaf", "sjsdlf", "apple juice", "banana", "mango", "sdlfkjasdlfjsadlfj", "lkjsdflsdfj");
final StringProperty currentSelection = new SimpleStringProperty(null);
final TextField fuzzySearchField = new TextField(null);
final CustomMenuItem fuzzySearchItem = new CustomMenuItem(fuzzySearchField, false);
fuzzySearchItem.setDisable(true);
// TODO unfortunately we seem to have to grab focus like this!
fuzzySearchField.addEventFilter(MouseEvent.MOUSE_MOVED, e->{fuzzySearchField.requestFocus(); fuzzySearchField.selectEnd();});
final ObservableList<String> currentMatches = FXCollections.observableArrayList();
// just some dummy matching here
fuzzySearchField.textProperty().addListener((obs, oldv, newv) -> currentMatches.setAll(options.stream().filter(s -> s.toLowerCase().contains(newv)).collect(Collectors.toList())));
final VBox labels = new VBox();
currentMatches.addListener((ListChangeListener<String>) change -> labels.getChildren().setAll(currentMatches.stream().map(MWE::label).collect(Collectors.toList())));
final CustomMenuItem labelItem = new CustomMenuItem(labels, false);
menu.getItems().setAll(fuzzySearchItem, labelItem);
fuzzySearchField.textProperty().addListener((obs, oldv, newv) -> currentSelection.setValue(currentMatches.size() > 0 ? currentMatches.get(0) : null));
fuzzySearchField.setText("");
menu.setOnShown(e -> fuzzySearchField.requestFocus());
final MenuButton button = new MenuButton("menu");
button.getItems().setAll(menu);
Platform.runLater(() -> {
final Scene scene = new Scene(button);
primaryStage.setScene(scene);
primaryStage.show();
});
}
}
To properly update lists dynamically in JavaFX you can use Binding with an observable list like I did so in your code. The only problem with the following code is it will not work as expected due the functionality of the Menu class, every time the list is updated the menu will hide. I think you should just style a list-view like discussed in the comments, and use the following binding again as it applies to all views that use Observable Lists I believe.
import javafx.application.Application;
import javafx.application.Platform;
import javafx.beans.binding.Bindings;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import java.lang.management.PlatformManagedObject;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class MWE2 extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) {
final Menu menu = new Menu("MENU");
final MenuButton button = new MenuButton("menu");
final List<String> options = Arrays.asList(
"AbC",
"dfjksdljf",
"skdlfj",
"stackoverflow");
final StringProperty currentSelection = new SimpleStringProperty(null);
final TextField fuzzySearchField = new TextField(null);
final CustomMenuItem fuzzySearchItem = new CustomMenuItem(fuzzySearchField, false);
// TODO unfortunately we seem to have to grab focus like this!
fuzzySearchField.addEventFilter(MouseEvent.MOUSE_MOVED, e -> {
fuzzySearchField.requestFocus();
fuzzySearchField.selectEnd();
});
final ObservableList<String> currentMatches = FXCollections.observableArrayList();
// just some dummy matching here
fuzzySearchField.textProperty().addListener((obs, oldv, newv) -> {
currentMatches.setAll(options.stream().filter(s -> s.toLowerCase().contains(newv)).collect(Collectors.toList()));
});
//changed from ArrayList to ObservableArray
ObservableList<MenuItem> items = FXCollections.observableArrayList();
currentMatches.addListener((ListChangeListener<String>) change -> {
items.clear();//Clearing items to in-case of duplicates and NULL duplicates.
items.add(fuzzySearchItem);
currentMatches.stream().map(MenuItem::new).forEach(items::add);
System.out.println("Updating menu items!");
menu.getItems().setAll(items);
});
// Binding to Observable items.
Bindings.bindContent(menu.getItems(), items);
fuzzySearchField.textProperty().addListener((obs, oldv, newv) -> currentSelection.setValue(currentMatches.size() > 0 ? currentMatches.get(0) : null));
fuzzySearchField.setText("");
button.getItems().setAll(menu);
final Scene scene = new Scene(button);
primaryStage.setScene(scene);
primaryStage.show();
}
}
Related
I can't find a way to add a simple "String" row to my tableView.
In fact i can add a row but its content is not visible ...
Here is my code:
#FXML
private TableView<String> table;
#FXML
private TableColumn<String, String> table2;
public ObservableList<String> getLanes()
{
ObservableList<String> lanes=FXCollections.observableArrayList();
lanes.add("TEST");
return lanes;
}
Then:
table.setItems(getLanes()); //Not working
and
table.getItems().add("TEST"); //Not working
But without success.
I read that and that as well as other documentations but it did not help me to do it in this simple way.
EDIT:
Adding this line solved my problem:
table2.setCellValueFactory(param -> new ReadOnlyStringWrapper(param.getValue()));
Here is a simple application where we are trying to load a single value into a TableView column. It also shows how to set a cellValueFactory() on a table column.
tableColumn.setCellValueFactory(param -> new ReadOnlyStringWrapper(param.getValue()));
MCVE
import javafx.application.Application;
import javafx.beans.property.ReadOnlyStringWrapper;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception {
TableView<String> tableView = new TableView<>();
TableColumn<String, String> tableColumn = new TableColumn<>("Name");
tableColumn.setCellValueFactory(param -> new ReadOnlyStringWrapper(param.getValue()));
tableView.getColumns().add(tableColumn);
ObservableList<String> items = FXCollections.observableArrayList("Itachi");
tableView.setItems(items);
VBox root = new VBox(tableView);
root.setAlignment(Pos.CENTER);
Scene scene = new Scene(root, 300, 275);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Is it possible to use a Node as a mouse cursor? I'm thinking in a ProgressIndicator. For example a determinate one, letting the user know how much percentage of the current task is done.
Probably the most reliable way to do this is to set the cursor to Cursor.NONE, and have a label with the progress indicator as its graphic, which tracks the mouse coordinates.
I tried using an ImageCursor which updated, but nothing appeared: I am guessing the images couldn't be computed quickly enough.
Here's an SSCCE of the first technique:
import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.concurrent.Service;
import javafx.concurrent.Task;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Cursor;
import javafx.scene.ImageCursor;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
public class ProgressIndicatorAsCursor extends Application {
#Override
public void start(Stage primaryStage) {
Button button = new Button("Start");
Service<Void> service = new Service<Void>() {
#Override
public Task<Void> createTask() {
return new Task<Void>() {
#Override
public Void call() throws Exception {
for (int i = 1 ; i <= 1000; i++) {
Thread.sleep(10);
updateProgress(i, 1000);
}
return null ;
}
};
}
};
button.disableProperty().bind(service.runningProperty());
button.setOnAction(e -> service.restart());
ProgressIndicator pi = new ProgressIndicator();
pi.progressProperty().bind(service.progressProperty());
Pane pane = new Pane();
// fill pane with rectangle as task progresses:
Rectangle rectangle = new Rectangle();
rectangle.setFill(Color.CORNFLOWERBLUE);
rectangle.setX(0);
rectangle.widthProperty().bind(pane.widthProperty());
rectangle.heightProperty().bind(pane.heightProperty().multiply(service.progressProperty()));
rectangle.yProperty().bind(pane.heightProperty().subtract(rectangle.heightProperty()));
pane.getChildren().add(rectangle);
Label label = new Label();
label.graphicProperty().bind(
Bindings.when(service.runningProperty())
.then(pi)
.otherwise((ProgressIndicator)null));
pane.setOnMouseEntered(e ->
pane.getChildren().add(label));
pane.setOnMouseExited(e ->
pane.getChildren().remove(label));
pane.setOnMouseMoved(e -> label.relocate(e.getX(), e.getY()));
pane.cursorProperty().bind(
Bindings.when(service.runningProperty())
.then(Cursor.NONE)
.otherwise(Cursor.DEFAULT));
BorderPane.setAlignment(button, Pos.CENTER);
BorderPane.setMargin(button, new Insets(10));
BorderPane root = new BorderPane(pane, new Rectangle(0,0,0,20), null, button, null);
primaryStage.setScene(new Scene(root, 400, 600));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Sees it's impossible, but you could obtain the cursor property and keep updating it with an image as your desire.
so maybe I'm not using the method how it's intended to be used but a video I watched by youtube user thenewboston used it exactly like this and it worked just fine. Help would be appreciated
package checkers;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.FlowLayout;
import javafx.scene.Scene ;
import javafx.scene.control.*;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.application.*;
import javafx.stage.*;
public class Checkers extends Application {
Stage window;
public void start(Stage primaryStage) throws Exception {
window = primaryStage;
window.setTitle("Title");
HBox layout = new HBox();
Button startButton = new Button("Start");
Button quitButton = new Button("Quit");
layout.getChildren().addAll(startButton, quitButton);
Scene startScene = new Scene(layout, 400, 300);
window.setScene(startScene);
window.show();
}
public static void main(String[] args) {
launch(args);
}
}
`
The error I am receiving is as follows -
"The method addAll(int, Collection) in the type List is not applicable for the arguments (Button, Button)"
You imported the wrong type of Button. You want import javafx.scene.control.Button; not import java.awt.Button;
I am using the following ControlFX project. Hence, created a Dialogs.java class in my package and pasted the code from there.
Since I am not Inside the package org.controlsfx.dialog , I have to do the following:
import org.controlsfx.dialog.LightweightDialog;
And I am getting the following error as shown in the image below:
When I went inside the package org.controlsfx.dialog and opened, LightweightDialog.class,
I wasn't able to make the class public.
How should I overcome this situation? Please advise.
If the class is not public, it is not part of the public API, so it's not intended (or really possible) for you to use.
To use a lightweight dialog in ControlsFX, you can either use the Dialogs class API and call the lightweight() method as part of the creation of your dialog, or you can call one of the Dialog constructors which takes a flag for the lightweight property.
Here's a complete example using the Dialogs fluent API:
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import org.controlsfx.dialog.Dialogs;
public class Main extends Application {
#Override
public void start(Stage primaryStage) {
try {
BorderPane root = new BorderPane();
Scene scene = new Scene(root,600,400);
TabPane tabPane = new TabPane();
Tab tab1 = new Tab("Tab 1");
BorderPane tab1Root = new BorderPane();
Button showDialogButton = new Button("Enter message...");
VBox messages = new VBox(3);
HBox buttons = new HBox(5);
buttons.setAlignment(Pos.CENTER);
buttons.setPadding(new Insets(5));
buttons.getChildren().add(showDialogButton);
tab1Root.setBottom(buttons);
ScrollPane messageScroller = new ScrollPane();
messageScroller.setContent(messages);
tab1Root.setCenter(messageScroller);
tab1.setContent(tab1Root);
Tab tab2 = new Tab("Tab 2");
tab2.setContent(new TextField("This is tab 2"));
tabPane.getTabs().addAll(tab1, tab2);
showDialogButton.setOnAction(event -> {
String response = Dialogs.create()
.lightweight()
.owner(tab1)
.masthead("Enter a new message")
.message("Enter your new message:")
.showTextInput();
if (response != null) {
messages.getChildren().add(new Label(response));
}
});
root.setCenter(tabPane);
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
Using the Dialog constructor you'd do something like this, though it's a lot more work:
// params are owner, title, lightweight:
Dialog dialog = new Dialog(someNode, "Dialog", true);
// lots of code here to configure dialog...
Action response = dialog.show();
The real beauty of ControlsFX is the very comprehensive documentation. Just check the Javadocs for Dialogs and for Dialog.
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.
You have to try this:-
listView.setPlaceholder(new Label("No Content In List"));
its 100% working....
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 ;
}
}
With fxml:
<ListView fx:id="foundContentList">
<placeholder>
<Label text="Nothing found" />
</placeholder>
</ListView>
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);