3

我想在 JavaFX 表的一列中设置不同的字体(实际上只是斜体)。
我已经看到通过调用 setCellFactory 然后在其上设置字体来回答这个问题。在我看来(JavaFX 经常出错 ;-))这是一种相当复杂且混乱的工作方式——如果你想处理单元格上的编辑等问题,你最终会得到一个大类,而不仅仅是使用类似的东西

col.setCellFactory(TextFieldTableCell.<TableRow>forTableColumn());

所以,我的问题是 - 有没有人设法在列上设置一个 id 或样式类并在 css 中引用它?

我的尝试是这样的(同时尝试 class 和 id ):

col.getStyleClass().add(colDef.isItalic()? "italic-cell" : null);
col.setId(colDef.isItalic()? "italic-cell" : null);

然后使用一些组合

#italic-cell
.italic-cell
#special-table .italic-cell

等等等等

-fx-font-style: italic;

作为 ID / 类的样式

4

1 回答 1

8

表格列的 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 上测试输出结果:

收款人

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 的行着色方法的信息位于:

于 2013-05-31T19:03:38.663 回答