gpt4 book ai didi

java - 在 JavaFx 中格式化 TableView 中的最后一行

转载 作者:太空宇宙 更新时间:2023-11-04 02:33:21 25 4
gpt4 key购买 nike

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

最佳答案

你可以这样做:

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 */
}

中南合作:

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 ;
}

关于java - 在 JavaFx 中格式化 TableView 中的最后一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36132186/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com