当我绘制列的背景然后我修复了单元格的大小时,当我隐藏列时会发生这种情况:
如果我评论这一行:“table.setFixedCellSize(24)”正常工作:
但我需要修复单元格的大小......¿这个错误有没有解决方案?
非常感谢所有人,抱歉我的英语不好。
你可以运行我的例子:
import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Test extends Application {
@Override
public void start(final Stage stage) {
// Create Items
final ObservableList<Person> data =
FXCollections.observableArrayList(new Person("Ruben", "Martin"),
new Person("Ruben", "Martin"), new Person("Ruben", "Martin"));
// Create columns
final TableColumn<Person, String> firstNameCol =
new TableColumn<>("First Name");
firstNameCol.setCellValueFactory(
new PropertyValueFactory<>("firstName"));
final TableColumn<Person, String> lastNameCol =
new TableColumn<>("Last Name");
lastNameCol.setCellValueFactory(
new PropertyValueFactory<>("lastName"));
// Create Table
final TableView<Person> table = new TableView<>();
table.setFixedCellSize(24);
table.setItems(data);
table.getColumns().addAll(firstNameCol, lastNameCol);
// Create CheckBox
final CheckBox checkLastName = new CheckBox();
checkLastName.setText("Last Name");
checkLastName.setSelected(true);
lastNameCol.setStyle("-fx-background-color:yellow");
lastNameCol.visibleProperty().
bindBidirectional(checkLastName.selectedProperty());
final VBox vbox = new VBox();
vbox.setSpacing(5);
vbox.setPadding(new Insets(10, 0, 0, 10));
vbox.getChildren().addAll(table, checkLastName);
final Scene scene = new Scene(new Group());
stage.setWidth(450);
stage.setHeight(550);
((Group) scene.getRoot()).getChildren().addAll(vbox);
stage.setScene(scene);
stage.show();
}
public static void main(final String[] args) {
launch(args);
}
public static class Person {
private final SimpleStringProperty firstName;
private final SimpleStringProperty lastName;
private Person(final String fName, final String lName) {
this.firstName = new SimpleStringProperty(fName);
this.lastName = new SimpleStringProperty(lName);
}
public String getFirstName() {
return this.firstName.get();
}
public void setFirstName(final String fName) {
this.firstName.set(fName);
}
public String getLastName() {
return this.lastName.get();
}
public void setLastName(final String fName) {
this.lastName.set(fName);
}
}
}
答案 0 :(得分:0)
可能这是一种解决方法但它有效:
StringBinding sb = Bindings.when(lastNameCol.visibleProperty())
.then("-fx-background-color:yellow")
.otherwise("");
lastNameCol.styleProperty().bind(sb);