问题描述
如何在java fx中为表格视图中的列设置字体.我有一个包含 3 列的表格,我想为每一列设置不同的字体.此外,是否可以在运行时更改字体.请帮忙提供一些示例代码.
How to set font to a column in table view in java fx. I have a table with 3 columns and i want to set different fonts for each of the columns. Also, is it possible to change the font in runtime. Please help with some sample code.
推荐答案
更改表格列样式:
您应该使用 TableColumn#setCellFactory() 自定义单元格项呈现.
例如,像这样 Person class 的数据模型:
Changing the table column style:
You should to use TableColumn#setCellFactory() to customize cell item rendering.
For example, datamodel with like this Person class:
// init code vs..
TableColumn firstNameCol = new TableColumn("First Name");
firstNameCol.setMinWidth(100);
firstNameCol.setCellValueFactory(new PropertyValueFactory<Person, String>("firstName"));
firstNameCol.setCellFactory(getCustomCellFactory("green"));
TableColumn lastNameCol = new TableColumn("Last Name");
lastNameCol.setMinWidth(100);
lastNameCol.setCellValueFactory(new PropertyValueFactory<Person, String>("lastName"));
lastNameCol.setCellFactory(getCustomCellFactory("red"));
table.setItems(data);
table.getColumns().addAll(firstNameCol, lastNameCol);
// scene create code vs..
和常见的getCustomCellFactory()
方法:
private Callback<TableColumn<Person, String>, TableCell<Person, String>> getCustomCellFactory(final String color) {
return new Callback<TableColumn<Person, String>, TableCell<Person, String>>() {
@Override
public TableCell<Person, String> call(TableColumn<Person, String> param) {
TableCell<Person, String> cell = new TableCell<Person, String>() {
@Override
public void updateItem(final String item, boolean empty) {
if (item != null) {
setText(item);
setStyle("-fx-text-fill: " + color + ";");
}
}
};
return cell;
}
};
}
更改表格列标题样式:
TableView
与 JavaFX 的其他控件一样,使用名为 caspian.css
的内置样式表,它与 jfxrt.jar 捆绑在一起.请参阅JavaFX 2 和 CSS 类"的答案.
要更改列字体样式,您可以覆盖默认样式或自定义它:
覆盖:
Changing the table column header style:
TableView
like other controls of JavaFX uses a built-in style sheet named caspian.css
which is bundled with jfxrt.jar. See the answer of "JavaFX 2 and CSS classes".
To change the column font style you can either override the default style or customize it:
Overriding:
.table-view .column-header{
-fx-text-fill: -fx-selection-bar-text;
-fx-font-size: 16;
-fx-font-family: "Arial";
}
定制:
#my-custom .table-view .column-header {
-fx-text-fill: red;
-fx-font-size: 26;
-fx-font-family: "Arial";
}
覆盖应用中所有 TableView
的效果.因此,如果您更喜欢自定义,那么一旦您定义了多种样式,您就可以在运行时将自定义样式应用到任何 tableview 中,
Overriding effects all TableView
s in app. So if you prefer to customize then once you define multiple styles, you can apply the custom style to any tableview at runtime as,
myTableView.setId("my-custom");
...
// Apply another style at runtime
myTableView.setId("my-custom2");
要了解如何定义样式表文件并将其加载到应用程序,请查看JavaFX 如何设置场景背景图像"的帖子.
To see how to define and load style sheet files to app, check out the post of "JavaFX How to set scene background image".
但是,我认为将不同的样式应用于不同的列需要更多的努力.
However, applying different styles to different columns requires more effort I think.
这篇关于在 javafx 中设置字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!