【问题标题】:Formatting Last Row in tableview in JavaFx在JavaFx中的tableview中格式化最后一行
【发布时间】:2016-03-21 13:21:51
【问题描述】:

我需要通过 css 格式化 JavaFX 中 tableview 中的最后一行,如果在 CSS 中不可能,那么如何在代码中做到这一点。

【问题讨论】:

  • 有 :last-child 选择器
  • @Goombah 你确定它是 javafx css 属性吗????
  • 不,我的错。它不是。最后一行的类名呢?

标签: java css javafx-8 fxml


【解决方案1】:

你可以这样做:

TableView<Item> table = new TableView<>();

// ...

PseudoClass lastRow = PseudoClass.getPseudoClass("last-row");

table.setRowFactory(tv -> new TableRow<Item>() {
    @Override
    public void updateIndex(int index) {
        super.updateIndex(index);
        pseudoClassStateChanged(lastRow, 
            index >= 0 && index == table.getItems().size() - 1);
    }
});

(显然将 Item 替换为您的实际模型类)现在在外部 css 文件中:

.table-row-cell:last-row {
    /* styles for last row here... */
}

.table-row-cell:last-row .table-cell {
    /* styles for cells in last row here, if needed */
}

SSCCE:

import java.util.Random;
import java.util.function.Function;

import javafx.application.Application;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.Property;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.css.PseudoClass;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableRow;
import javafx.scene.control.TableView;
import javafx.stage.Stage;

public class TableViewStyleLastRow extends Application {

    @Override
    public void start(Stage primaryStage) {
        TableView<Item> table = new TableView<>();
        table.getColumns().add(column("Item", Item::nameProperty));
        table.getColumns().add(column("Value", Item::valueProperty));

        PseudoClass lastRow = PseudoClass.getPseudoClass("last-row");
        table.setRowFactory(tv -> new TableRow<Item>(){
            @Override
            public void updateIndex(int index) {
                super.updateIndex(index);
                pseudoClassStateChanged(lastRow, index >=0 && index == table.getItems().size() - 1);
            }
        });

        Random rng = new Random();
        for (int i = 1 ; i <= 25 ; i++) {
            table.getItems().add(new Item("Item "+i, rng.nextInt(200)));
        }

        Scene scene = new Scene(table);
        scene.getStylesheets().add("style-last-row.css");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private static <S,T> TableColumn<S,T> column(String title, Function<S, Property<T>> prop) {
        TableColumn<S, T> col = new TableColumn<>(title);
        col.setCellValueFactory(cellData -> prop.apply(cellData.getValue()));
        return col ;
    }

    public static class Item {
        private final StringProperty name = new SimpleStringProperty();
        private final IntegerProperty value = new SimpleIntegerProperty();

        public Item(String name, int value) {
            setName(name);
            setValue(value);
        }

        public final StringProperty nameProperty() {
            return this.name;
        }


        public final java.lang.String getName() {
            return this.nameProperty().get();
        }


        public final void setName(final java.lang.String name) {
            this.nameProperty().set(name);
        }


        public final IntegerProperty valueProperty() {
            return this.value;
        }


        public final int getValue() {
            return this.valueProperty().get();
        }


        public final void setValue(final int value) {
            this.valueProperty().set(value);
        }



    }

    public static void main(String[] args) {
        launch(args);
    }
}

使用 style-last-row.css:

.table-row-cell:last-row {
    -fx-background: #707070 ;
}

【讨论】:

  • 非常感谢@James_D,很好的回答。
猜你喜欢
  • 2015-02-01
  • 2015-11-12
  • 2015-12-02
  • 2017-04-03
  • 1970-01-01
  • 2015-07-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多