我想在JavaFX表的一列中设置不同的字体(实际上只是斜体).
通过调用setCellFactory然后在其上设置字体,我看到了这个问题.在我看来(这与JavaFX一直是错误的;-))这是一个相当混乱的工作方式 - 如果你想处理单元格上的编辑等,你最终会得到一个大类而不是只是使用类似的东西
col.setCellFactory(TextFieldTableCell.<TableRow>forTableColumn());
Run Code Online (Sandbox Code Playgroud)
所以,我的问题是 - 有人设法在列上设置id或样式类并在css中引用它吗?
我的尝试是这样的(尝试类和id):
col.getStyleClass().add(colDef.isItalic()? "italic-cell" : null);
col.setId(colDef.isItalic()? "italic-cell" : null);
Run Code Online (Sandbox Code Playgroud)
然后使用一些组合
#italic-cell
.italic-cell
#special-table .italic-cell
Run Code Online (Sandbox Code Playgroud)
等等
-fx-font-style: italic;
Run Code Online (Sandbox Code Playgroud)
作为ID /类的样式
表格的CSS样式
在列中添加适当的样式类:
nameColumn.getStyleClass().add("italic");
Run Code Online (Sandbox Code Playgroud)
在场景中添加一个样式表,用于定义表格单元格的斜体样式:
.italic.table-cell { -fx-font-style: italic; }
Run Code Online (Sandbox Code Playgroud)
警告1
我再次根据T-and-M Mike的评论尝试了这个例子,事实上它在使用Java 8的操作系统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;
}
Run Code Online (Sandbox Code Playgroud)
警告2
在OS X 10.9上的Java 8b132上,当表首次显示表格内容与表格标题略有不同时,会出现轻微的渲染错误.
table.requestLayout();在屏幕上显示表格之后调用似乎修复了表格的对齐渲染错误,但如果一切正常,则无需调用.
可执行样本
在Win7 + Java 8b91上测试输出结果:

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();
}
}
Run Code Online (Sandbox Code Playgroud)
Friend.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; }
}
Run Code Online (Sandbox Code Playgroud)
细胞shader.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;
}
Run Code Online (Sandbox Code Playgroud)
基于细胞工厂的方法
作为参考(并且如上所示,对于简单的样式操作不是必需的),关于基于自定义TableCell的行着色方法的信息在: