【问题标题】:JavaFx How to set row background color of specifics rows in TableViewJavaFx如何在TableView中设置特定行的行背景颜色
【发布时间】:2017-12-02 05:11:26
【问题描述】:

我正在使用具有 2 列的 TabelView:

@FXML
private TableView<Person> personTable;
@FXML
private TableColumn<Person, String> firstNameColumn;
@FXML
private TableColumn<Person, String> lastNameColumn;


public class Person {

    private final StringProperty firstName;
    private final StringProperty lastName;


    /**
     * Default constructor.
     */
    public Person() {
        this(null, null);
    }

    /**
     * Constructor with some initial data.
     * 
     * @param firstName
     * @param lastName
     */
    public Person(String firstName, String lastName) {
        this.firstName = new SimpleStringProperty(firstName);
        this.lastName = new SimpleStringProperty(lastName);        
    }

    public String getFirstName() {
        return firstName.get();
    }

    public void setFirstName(String firstName) {
        this.firstName.set(firstName);
    }

    public StringProperty firstNameProperty() {
        return firstName;
    }

    public String getLastName() {
        return lastName.get();
    }

    public void setLastName(String lastName) {
        this.lastName.set(lastName);
    }

    public StringProperty lastNameProperty() {
        return lastName;
    }


}

我有一个计算行号的逻辑,我需要将此行的背景设置为红色(每隔几秒我计算一次行,我需要将计算后的行背景设置为红色。

我检查了那些 Q:

Colouring table row in JavaFX

但它确实有帮助。 所以我可以设置一个随机的行,它的背景颜色?

【问题讨论】:

    标签: java javafx


    【解决方案1】:

    根据您的具体需要,您可以执行以下操作:

    ObjectProperty<Person> criticalPerson = new SimpleObjectProperty<>();
    
    personTable.setRowFactory(tv -> {
        TableRow<Person> row = new TableRow<>();
        BooleanBinding critical = row.itemProperty().isEqualTo(criticalPerson);
        row.styleProperty().bind(Bindings.when(critical)
            .then("-fx-background-color: red ;")
            .otherwise(""));
        return row ;
    });
    

    这是一个 SSCCE:

    import java.util.function.Function;
    
    import javafx.application.Application;
    import javafx.beans.binding.Bindings;
    import javafx.beans.binding.BooleanBinding;
    import javafx.beans.property.ObjectProperty;
    import javafx.beans.property.SimpleObjectProperty;
    import javafx.beans.property.SimpleStringProperty;
    import javafx.beans.property.StringProperty;
    import javafx.beans.value.ObservableValue;
    import javafx.geometry.Insets;
    import javafx.geometry.Pos;
    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.BorderPane;
    import javafx.scene.layout.HBox;
    import javafx.stage.Stage;
    
    public class HighlightTableRows extends Application {
    
        @Override
        public void start(Stage primaryStage) {
    
    
            TableView<Person> table = new TableView<>();
            table.getColumns().add(column("First Name", Person::firstNameProperty));
            table.getColumns().add(column("Last Name", Person::lastNameProperty));
    
            for (int i = 1 ; i <=50 ; i++) {
                table.getItems().add(new Person("Person"+i, "McPerson"+i));
            }
    
            ObjectProperty<Person> criticalPerson = new SimpleObjectProperty<>();
    
    
            table.setRowFactory(tv -> {
                TableRow<Person> row = new TableRow<>();
                BooleanBinding critical = row.itemProperty().isEqualTo(criticalPerson).and(row.itemProperty().isNotNull());
                row.styleProperty().bind(Bindings.when(critical)
                    .then("-fx-background-color: red ;")
                    .otherwise(""));
                return row ;
            });
    
            BorderPane root = new BorderPane(table);
    
            Button apply = new Button("Make critical");
            apply.setOnAction(e -> criticalPerson.set(table.getSelectionModel().getSelectedItem()));
            apply.disableProperty().bind(table.getSelectionModel().selectedItemProperty().isNull());
            Button clear = new Button("Clear");
            clear.setOnAction(e -> criticalPerson.set(null));
    
            HBox controls = new HBox(5, apply, clear);
            controls.setAlignment(Pos.CENTER);
            controls.setPadding(new Insets(5));
            root.setBottom(controls);
    
            Scene scene = new Scene(root);
            primaryStage.setScene(scene);
            primaryStage.show();
        }
    
        private <S,T> TableColumn<S,T> column(String text, Function<S, ObservableValue<T>> prop) {
            TableColumn<S,T> col = new TableColumn<>(text);
            col.setCellValueFactory(cellData -> prop.apply(cellData.getValue()));
            return col ;
        }
    
        public class Person {
    
            private final StringProperty firstName;
            private final StringProperty lastName;
    
            /**
             * Default constructor.
             */
            public Person() {
                this(null, null);
            }
    
            /**
             * Constructor with some initial data.
             * 
             * @param firstName
             * @param lastName
             */
            public Person(String firstName, String lastName) {
                this.firstName = new SimpleStringProperty(firstName);
                this.lastName = new SimpleStringProperty(lastName);
            }
    
            public String getFirstName() {
                return firstName.get();
            }
    
            public void setFirstName(String firstName) {
                this.firstName.set(firstName);
            }
    
            public StringProperty firstNameProperty() {
                return firstName;
            }
    
            public String getLastName() {
                return lastName.get();
            }
    
            public void setLastName(String lastName) {
                this.lastName.set(lastName);
            }
    
            public StringProperty lastNameProperty() {
                return lastName;
            }
    
        }
    
        public static void main(String[] args) {
            launch(args);
        }
    }
    

    如果多行可能需要同时变为红色,请使用包含行应为红色的项目的ObservableList 进行明显修改,等等。您还可以考虑在模型类中添加BooleanProperty 并让表格行观察它。

    【讨论】:

    • 我错过了一些东西。 1.它没有编译。 2.我看不到如何将计算的行号转移到这个?
    • @user3668129 编译错误是什么?对于第二个问题,只需执行 criticalPerson.set(personTable.getItems().get(rowNumber));(或 criticalPerson.set(person);,如果更容易计算实际人员而不是他们的索引)。
    • “何时”无法识别
    • 听起来您对 Bindings 的导入错误?
    • 导入 javafx.beans.binding.BooleanBinding;导入 javafx.beans.property.ObjectProperty;导入 javafx.beans.property.SimpleObjectProperty;
    猜你喜欢
    • 2017-04-30
    • 2015-03-26
    • 2011-02-18
    • 1970-01-01
    • 2014-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-21
    相关资源
    最近更新 更多