表格列的 CSS 样式
为您的列添加适当的样式类:
nameColumn.getStyleClass().add("italic");
向场景添加样式表,定义表格单元格的斜体样式:
.italic.table-cell { -fx-font-style: italic; }
警告 1
我根据 T-and-M Mike 的评论再次尝试了这个示例,实际上它在带有 Java 8 的 OS X 10.9 上并不能很好地工作。我相信,在 Windows 上,只需将斜体设置-fx-font-style
为斜体就可以让文本以斜体显示。在 Mac 上,这会将字体设置为 System Italic,这是一种似乎不存在的字体,因此文本仅显示没有斜体。如果您还将字体系列设置为定义了斜体的某些字体,则列中的文本将按预期以斜体显示。例如,使用 Times New Roman,斜体:
.italic.table-cell {
-fx-font-family: "Times New Roman";
-fx-font-style: italic;
}
警告 2
在 OS X 10.9 上的 Java 8b132 上,当第一次显示表格时,表格内容与表格标题略有不同,存在轻微的渲染错误。
在屏幕上显示表格后调用table.requestLayout();
似乎修复了表格的对齐渲染错误,但如果一切正常,则不需要调用。
可执行样本
在 Win7 + Java 8b91 上测试输出结果:
![收款人](https://i.stack.imgur.com/WKRZm.png)
CellShadedTable.java
import javafx.application.Application;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;
// demonstrates styling column cells in a tableview.
public class CellShadedTable extends Application {
public static void main(String[] args) throws Exception { launch(args); }
@Override public void start(final Stage stage) throws Exception {
stage.setTitle("So called friends . . .");
// create a table.
TableColumn<Friend, String> nameColumn = new TableColumn<>("Name");
nameColumn.setCellValueFactory(new PropertyValueFactory<Friend, String>("name"));
nameColumn.setPrefWidth(75);
nameColumn.getStyleClass().add("italic");
TableColumn<Friend, String> owesMeColumn = new TableColumn<>("Owes Me");
owesMeColumn.setCellValueFactory(new PropertyValueFactory<Friend, String>("owesMe"));
owesMeColumn.setPrefWidth(150);
TableColumn<Friend, Boolean> willPayColumn = new TableColumn<>("Will Pay Up");
willPayColumn.setCellValueFactory(new PropertyValueFactory<Friend, Boolean>("willPay"));
willPayColumn.setPrefWidth(75);
TableView<Friend> table = new TableView(Friend.data);
table.getColumns().addAll(
nameColumn,
owesMeColumn,
willPayColumn
);
table.setPrefHeight(200);
table.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
stage.setScene(new Scene(table));
stage.getScene().getStylesheets().add(getClass().getResource("cell-shader.css").toExternalForm());
stage.show();
table.requestLayout();
}
}
朋友.java
import javafx.collections.*;
/** Sample data for a table view */
public class Friend {
final static public ObservableList data = FXCollections.observableArrayList(
new Friend("George", "Movie Ticket", true),
new Friend("Irene", "Pay Raise", false),
new Friend("Ralph", "Return my Douglas Adams Books", false),
new Friend("Otto", "Game of Golf", true),
new Friend("Sally", "$12,045.98", false),
new Friend("Antoine", "Latte", true)
);
final private String name;
final private String owesMe;
final private boolean willPay;
public Friend(String name, String owesMe, boolean willPay) {
this.name = name; this.owesMe = owesMe; this.willPay = willPay;
}
public String getName() { return name; }
public String getOwesMe() { return owesMe; }
public boolean getWillPay() { return willPay; }
}
单元格着色器.css
/** file: cell-shader.css
place in same directory as CellShadedTable.java */
.italic.table-cell {
-fx-font-family: "Times New Roman";
-fx-font-style: italic;
}
基于细胞工厂的方法
作为参考(如上所示,简单的样式操作不需要),有关基于自定义 TableCell 的行着色方法的信息位于: