如何在不使用CSS的情况下更改列标题的字体



我希望能够在不使用CSS的情况下为JavaFX的TableView更改列标题的字体。我在网上做了很多搜索,所有的解决方案似乎都只使用CSS。我想改变它的字体是一个自定义的,并在CSS中加载自定义字体JavaFX带来了一个错误,我似乎无法修复搜索数十个链接后。

我尝试的CSS方法是

@font-face {
font-family: '8bitoperator';
src: url('assets/8bitoperator.ttf');
}
.table-view .column-header.foo .label {
-fx-text-fill: white;
-fx-font-size: 0.13%; /* arbitrary value */
-fx-font-family: '8bitoperator';
}

遇到loadStylesheetUnPrivileged错误。我的所有其他样式在我的程序已经通过Javafx而不是CSS,所以我想保持它。

任何帮助都是感激的。

我不确定您当前的样式表问题。但是,如果您正在寻找一种以编程方式更新列标题字体的方法,下面是一种方法。话虽如此,还可以有许多其他方法。

一般的想法是:你想要的节点,不能通过任何通用的API访问。因此,当表视图完全呈现时,我们查找节点。"needsLayout"属性将在节点完全渲染时变为false。因此,如果你依靠那个属性和一个额外的属性来避免多次运行,你可以使用"lookupAll"方法。

import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.Priority;
import javafx.scene.layout.Region;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import java.util.concurrent.atomic.AtomicBoolean;
public class ColumnHeaderCustomFontDemo extends Application {
boolean fontsLoaded = false;
int i = 1;
@Override
public void start(Stage primaryStage) throws Exception {
TableView<Person> tableView = new TableView<>();
TableColumn<Person, String> fnCol = new TableColumn<>("First Name");
fnCol.setPrefWidth(120);
fnCol.setId("firstColumn");
TableColumn<Person, String> lnCol = new TableColumn<>("Last Name");
lnCol.setId("lastColumn");
lnCol.setVisible(false);
TableColumn<Person, String> cityCol = new TableColumn<>("City");
cityCol.setId("cityColumn");
tableView.getColumns().addAll(fnCol, lnCol, cityCol);
tableView.needsLayoutProperty().addListener((obs, old, needsLayout) -> {
if (!needsLayout) {
AtomicBoolean updateLayout = new AtomicBoolean();
tableView.lookupAll(".column-header").stream().map(node -> (Region) node).forEach(column -> {
if (column.getProperties().get("fontLoaded") == null) {
switch (column.getId()) {
case "firstColumn":
updateFont(column, "Calibri", 20);
updateLayout.set(true);
break;
case "lastColumn":
updateFont(column, "Verdana", 10);
updateLayout.set(true);
break;
case "newColumn":
updateFont(column, "Verdana", 25);
updateLayout.set(true);
break;
}
column.getProperties().put("fontLoaded", true);
}
});
if (updateLayout.get()) {
// A timer to execute only once in the next pulse.
new AnimationTimer(){
int count = 0;
@Override
public void handle(long now) {
if(count>1){
tableView.requestLayout();
stop();
}
count++;
}
}.start();
}
}
});
CheckBox showLastColumn = new CheckBox("Show LastName column");
showLastColumn.selectedProperty().addListener((obs, old, val) -> lnCol.setVisible(val));
Button addColumn = new Button("Add Column");
addColumn.setOnAction(e -> {
TableColumn<Person, String> newCol = new TableColumn<>("Column " + i++);
newCol.setId("newColumn");
tableView.getColumns().add(newCol);
});
VBox root = new VBox();
root.setSpacing(10);
root.setPadding(new Insets(10));
root.getChildren().addAll(showLastColumn, addColumn, tableView);
VBox.setVgrow(tableView, Priority.ALWAYS);
Scene sc = new Scene(root, 500, 500);
primaryStage.setScene(sc);
primaryStage.setTitle("ColumnHeader Custom Font");
primaryStage.show();
}
private void updateFont(Region column, String fontName, int fontSize) {
column.lookupAll(".label").stream().map(node -> (Label) node).forEach(label -> label.setFont(new Font(fontName, fontSize)));
}
class Person {
// TableView item
}
}

相关内容

最新更新