No, you cannot hide a table only when it is empty using only CSS.
In Java code you can do:
table.managedProperty().bind(table.visibleProperty());
table.visibleProperty().bind(Bindings.isEmpty(table.getItems()).not());
Managed binding is only done if you do not want your invisible table to occupy layout space.
Application example
Here you can play to understand how visibility and controlled settings work.

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;
}
}
}
source
share