【问题标题】:JavaFX table view rows colorJavaFX 表视图行颜色
【发布时间】: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 属性,它也不起作用。

有谁知道如何解决这个问题以及为什么它不起作用?

【问题讨论】:

    标签: java javafx javafx-8


    【解决方案1】:

    滚动时,表格可能需要再创建几行。如果在按下按钮后创建行,您首先将新行的disable 属性绑定到您创建的布尔属性(因此该行的禁用属性设置为 true),然后您使用更改样式的行的禁用属性注册一个侦听器。由于该行的 disable 属性在监听器注册后永远不会改变,因此它永远不会被调用,样式也永远不会改变。

    你可以的

        table.setRowFactory(param ->
        {
            TableRow<Integer> row = new TableRow<>();
    
            row.disableProperty().addListener((observable, oldValue, newValue) ->
            {
                if (newValue)
                    row.setStyle("-fx-background-color: red");
                else
                    row.setStyle("");
            });
    
            row.disableProperty().bind(disable);
    
            return row;
        });
    

    或者你可以直接使用绑定:

        table.setRowFactory(param ->
        {
            TableRow<Integer> row = new TableRow<>();
    
            row.styleProperty().bind(Bindings
                .when(disable)
                .then("-fx-background-color: red;")
                .otherwise(""));
    
            row.disableProperty().bind(disable);
    
            return row;
        });
    

    或者你可以使用外部样式表

    .table-row-cell:disabled {
        -fx-background-color:red ;
    }
    

    并完全省略样式的侦听器/绑定:

    table.setRowFactory(param ->
    {
        TableRow<Integer> row = new TableRow<>();
    
        row.disableProperty().bind(disable);
    
        return row;
    });
    

    【讨论】:

      猜你喜欢
      • 2012-11-21
      • 1970-01-01
      • 2018-12-24
      • 2016-01-01
      • 1970-01-01
      • 2014-12-28
      • 2017-04-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多