【发布时间】:2016-11-09 12:48:18
【问题描述】:
我有这个简单的代码:
import javafx.application.Application;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableRow;
import javafx.scene.control.TableView;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Example extends Application
{
@Override
public void start(Stage stage) throws Exception
{
TableView<Integer> table = new TableView<>();
TableColumn<Integer, Integer> column = new TableColumn<>();
column.setCellValueFactory(param -> new SimpleObjectProperty<>(param.getValue()));
ObservableList<Integer> items = FXCollections.observableArrayList();
table.getColumns().add(column);
table.setItems(items);
table.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
for (int i = 0; i < 500; i++)
items.add((int) (Math.random() * 500));
BooleanProperty disable = new SimpleBooleanProperty();
table.setRowFactory(param ->
{
TableRow<Integer> row = new TableRow<>();
row.disableProperty().bind(disable);
row.disableProperty().addListener((observable, oldValue, newValue) ->
{
if (newValue)
row.setStyle("-fx-background-color: red");
else
row.setStyle("");
});
return row;
});
Button button = new Button("Disable all rows");
button.setOnAction(event -> disable.set(true));
stage.setScene(new Scene(new VBox(table, button)));
stage.show();
}
public static void main(String[] args)
{
launch(args);
}
}
当我运行它并按下按钮时,它应该为所有行着色。它有效,直到您向下滚动表格。然后一些奇怪的事情开始发生,有些行不会是彩色的,当你向后滚动时,它们会是彩色的,而另一些则不会。
但是当您先滚动然后按下按钮时,它会正常工作。我不知道那里发生了什么,这对我来说似乎是一个错误。
在原始代码中,我需要禁用某些行,因为我需要禁用表格复选框。然而,即使我们将 disable 属性切换为 editable 属性,它也不起作用。
有谁知道如何解决这个问题以及为什么它不起作用?
【问题讨论】: