JavaFX表列与css的字体不同
|
我想在
JavaFX表的一列中设置不同的字体(实际上只是斜体).
通过调用setCellFactory然后在其上设置字体,我看到了这个问题.在我看来(这与JavaFX一直是错误的;-))这是一个相当混乱的工作方式 – 如果你想处理单元格上的编辑等,你最终会得到一个大类而不是只是使用类似的东西 col.setCellFactory(TextFieldTableCell.<TableRow>forTableColumn()); 所以,我的问题是 – 有人设法在列上设置id或样式类并在css中引用它吗? 我的尝试是这样的(尝试类和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 /类的样式 解决方法
表格的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设置为italic就可以了以斜体显示.在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();
}
}
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",new Friend("Otto","Game of Golf",new Friend("Sally","$12,045.98",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; }
}
细胞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;
}
基于细胞工厂的方法 作为参考(并且如上所示,对于简单的样式操作不是必需的),关于基于自定义TableCell的行着色方法的信息在: > Updating TableView row appearance (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
