Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaFx:空ListView的默认消息_Java_Listview_Javafx_Javafx 2_Javafx 8 - Fatal编程技术网

JavaFx:空ListView的默认消息

JavaFx:空ListView的默认消息,java,listview,javafx,javafx-2,javafx-8,Java,Listview,Javafx,Javafx 2,Javafx 8,当任何表中没有记录时,它会显示一条消息“表中没有内容”,这是JavaFx中TableView的默认功能 所以我的问题是,JavaFx中的ListView是否也可以实现同样的功能?例如,如果任何ListView中没有任何项,则它将显示与TableView相同的消息,而不是空白/空字段。不完全确定,但我认为ListView没有设置占位符的方法(在表中没有内容时设置默认消息) 我使用的解决方法是在列表中创建一个表示“无内容”的对象,并在listview上显示该对象,同时禁用它 例如: Observa

当任何表中没有记录时,它会显示一条消息“表中没有内容”,这是JavaFx中TableView的默认功能


所以我的问题是,JavaFx中的ListView是否也可以实现同样的功能?例如,如果任何ListView中没有任何项,则它将显示与TableView相同的消息,而不是空白/空字段。

不完全确定,但我认为ListView没有设置占位符的方法(在表中没有内容时设置默认消息)

我使用的解决方法是在列表中创建一个表示“无内容”的对象,并在listview上显示该对象,同时禁用它

例如:


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

JavaFX8有一个用于ListView的setPlaceholder(…)方法

在早期版本中,您需要以某种方式推出自己的。这有点像黑客:它将ListView包装在堆栈窗格中,在列表视图顶部显示一个白色矩形和占位符。占位符和矩形的visible属性已绑定,因此它们仅在列表为空时可见

也许有更简单的方法我现在还没有看到

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 ;
    }
}
导入javafx.application.application;
导入javafx.beans.binding.Bindings;
导入javafx.beans.property.IntegerProperty;
导入javafx.beans.property.SimpleIntegerProperty;
导入javafx.event.ActionEvent;
导入javafx.event.EventHandler;
导入javafx.geometry.Insets;
导入javafx.scene.Node;
导入javafx.scene.scene;
导入javafx.scene.control.Button;
导入javafx.scene.control.Label;
导入javafx.scene.control.ListView;
导入javafx.scene.layout.BorderPane;
导入javafx.scene.layout.HBox;
导入javafx.scene.layout.StackPane;
导入javafx.scene.paint.Color;
导入javafx.scene.shape.Rectangle;
导入javafx.stage.stage;
公共类ListViewPlaceholder测试扩展了应用程序{
@凌驾
公共无效开始(阶段primaryStage){
最终ListView ListView=新建ListView();
最终IntegerProperty计数器=新的SimpleIntegerProperty();
最终按钮addButton=新按钮(“添加项目”);
addButton.setOnAction(新的EventHandler(){
@凌驾
公共无效句柄(ActionEvent事件){
counter.set(counter.get()+1);
添加(“项”+计数器.get());
}
});
最终按钮移除按钮=新按钮(“移除”);
removeButton.setOnAction(新的EventHandler(){
@凌驾
公共无效句柄(ActionEvent事件){
移除(listView.getSelectionModel().getSelectedIndex());
}
});
removeButton.disableProperty().bind(Bindings.equal(listView.getSelectionModel().selectedIndexProperty(),-1));
最终HBox按钮=新HBox(5);
按钮。设置填充(新插图(10));
buttons.getChildren().addAll(addButton,removeButton);
最终边界窗格根=新边界窗格();
setCenter(createPlaceholderForListView(listView,新标签(“列表中没有内容”));
root.setBottom(按钮);
最终场景=新场景(根,600400);
初级阶段。场景(场景);
primaryStage.show();
}
公共静态void main(字符串[]args){
发射(args);
}
私有节点createPlaceholderForListView(ListView ListView,节点占位符){
最终堆栈窗格=新堆栈窗格();
最终矩形rect=新矩形(0,0,颜色.白色);
rect.widthProperty().bind(listView.widthProperty());
rect.heightProperty().bind(listView.heightProperty());
pane.getChildren().addAll(listView、rect、占位符);
占位符.visibleProperty().bind(Bindings.isEmpty(listView.getItems());
rect.visibleProperty().bind(占位符.visibleProperty());
rect.setMouseTransparent(true);
返回窗格;
}
}

您必须尝试以下方法:-

listView.setPlaceholder(新标签(“列表中无内容”)

使用fxml时,其100%的工作效率…

<ListView fx:id="foundContentList">
    <placeholder>
        <Label text="Nothing found" />
    </placeholder>
</ListView>


你是对的,.setPlaceholder(..)方法很好地取代了所有的hack,并且很容易解决我上面提到的问题。只要你处于可以使用JavaFX 8的情况下,这就是最好的方法。