我有TableView
并希望只有在其中有任何行时才能显示它。可以用css做到吗?
答案 0 :(得分:4)
不,只有在使用CSS时才能将表隐藏起来。
在Java代码中,您可以这样做:
table.managedProperty().bind(table.visibleProperty());
table.visibleProperty().bind(Bindings.isEmpty(table.getItems()).not());
托管绑定只有在您不希望不可见表占用布局空间时才会显示。
示例应用
您可以通过以下方式了解可见性和托管设置的工作原理。
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;
}
}
}