css - JavaFX:如何隐藏空表?

标签 css javafx tableview

我有一个 TableView 并且想让它只有在其中有任何行时才可见。可以用 css 来实现吗?

最佳答案

不,您不能仅在表格为空时仅使用 CSS 隐藏表格。

在 Java 代码中,你可以这样做:

table.managedProperty().bind(table.visibleProperty());
table.visibleProperty().bind(Bindings.isEmpty(table.getItems()).not());

托管绑定(bind)仅适用于您不希望不可见表格占用布局空间的情况。

示例应用

您可以尝试了解可见性和托管设置的工作原理。

addressbook

import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.*;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;

public class TableViewHiding extends Application {

    private TableView<Person> table = new TableView<>();
    private final ObservableList<Person> data =
        FXCollections.observableArrayList(
            new Person("Jacob", "Smith", "jacob.smith@example.com"),
            new Person("Isabella", "Johnson", "isabella.johnson@example.com"),
            new Person("Ethan", "Williams", "ethan.williams@example.com"),
            new Person("Emma", "Jones", "emma.jones@example.com"),
            new Person("Michael", "Brown", "michael.brown@example.com")
        );

    private int addIdx = 0;

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

    @Override
    public void start(Stage stage) {
        final Label label = new Label("Address Book");
        label.setFont(new Font("Arial", 20));

        TableColumn<Person, String> firstNameCol = new TableColumn<>("First Name");
        firstNameCol.setMinWidth(100);
        firstNameCol.setCellValueFactory(
                new PropertyValueFactory<>("firstName"));

        TableColumn<Person, Boolean> highlightCol = new TableColumn<>("");
        highlightCol.setPrefWidth(10);
        highlightCol.setResizable(false);
        highlightCol.setCellValueFactory(
                new PropertyValueFactory<>("highlighted"));

        TableColumn<Person, String> lastNameCol = new TableColumn<>("Last Name");
        lastNameCol.setMinWidth(100);
        lastNameCol.setCellValueFactory(
                new PropertyValueFactory<>("lastName"));

        TableColumn<Person, String> emailCol = new TableColumn<>("Email");
        emailCol.setMinWidth(200);
        emailCol.setCellValueFactory(
                new PropertyValueFactory<>("email"));

        table.setItems(FXCollections.observableArrayList(data));
        table.getColumns().addAll(highlightCol, firstNameCol, lastNameCol, emailCol);

        final Button add = new Button("Add row");
        add.setMaxWidth(Double.MAX_VALUE);
        add.setOnAction(event -> {
            addIdx = (addIdx + 1) % data.size();
            table.getItems().add(data.get(addIdx));
        });

        final Button remove = new Button("Remove selected row");
        remove.setMaxWidth(Double.MAX_VALUE);
        remove.setOnAction(event ->
                table.getItems().remove(
                        table.getSelectionModel().getSelectedItem()
                )
        );
        remove.disableProperty().bind(
                Bindings.isEmpty(
                        table.getSelectionModel().getSelectedCells()
                )
        );
        final CheckBox manage = new CheckBox("Bind layout management and visibility");
        manage.selectedProperty().addListener((observable, wasSelected, isSelected) -> {
            if (isSelected) {
                table.setManaged(table.isVisible());
                table.managedProperty().bind(table.visibleProperty());
            } else {
                table.managedProperty().unbind();
                table.setManaged(true);
            }
        });

        table.visibleProperty().bind(Bindings.isEmpty(table.getItems()).not());

        final VBox layout = new VBox();
        layout.setAlignment(Pos.TOP_CENTER);
        layout.setSpacing(5);
        layout.setPadding(new Insets(10));
        layout.getChildren().addAll(
                label,
                table,
                remove,
                add,
                manage
        );

        Scene scene = new Scene(layout);
        stage.setScene(scene);
        stage.show();
    }

    public static class Person {
        private final SimpleStringProperty firstName;
        private final SimpleStringProperty lastName;
        private final SimpleStringProperty email;

        private Person(String fName, String lName, String email) {
            this.firstName = new SimpleStringProperty(fName);
            this.lastName = new SimpleStringProperty(lName);
            this.email = new SimpleStringProperty(email);
        }

        public SimpleStringProperty firstNameProperty() {
            return firstName;
        }

        public SimpleStringProperty lastNameProperty() {
            return lastName;
        }

        public SimpleStringProperty emailProperty() {
            return email;
        }
    }
}

关于css - JavaFX:如何隐藏空表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25378450/

相关文章:

ios - Storyboard中具有不同单元格大小和单元格内容的 UITableViewController

ios - UITableView点击不调用didselectrowAtIndexpath(最后一个单元格在ipad中不可点击)ios Xcode 6.2

swift - editActionsForRowAt IndexPath - 如何在操作完成后关闭 slider

html - Safari 浏览器的背景色保持白色

javascript - 在循环中多次覆盖元素的样式?

asp.net - 无法向我的 <asp :Label>? 添加样式

单击时的 JavaFX 形状

html - 显示内联 block 不起作用,第二个 div 出现在下方,并且在检查元素上显示为显示 : block

JavaFX:从组中删除文本会导致异常

java - 如何拦截弹出窗口的隐藏以验证字段