[英]Change ListView font size in JavaFX
I want to know how can I change the list view items text font size in JavaFx.我想知道如何在 JavaFx 中更改列表视图项文本字体大小。 The size of each row text will vary .
每行文本的大小会有所不同。 I tried using cell factor property but i don't know how to use it.
我尝试使用细胞因子属性,但我不知道如何使用它。 Can anybody help me here ?
有人可以帮我吗?
A similar question is here:
一个类似的问题在这里:
How to change the font size in ListView in JavaFX? 如何在 JavaFX 中更改 ListView 中的字体大小?
Also you can play with the .css
below:您也可以使用下面的
.css
:
.list-cell:filled:selected:focused, .list-cell:filled:selected {
-fx-background-color: linear-gradient(#328BDB 0.0%, #207BCF 25.0%, #1973C9 75.0%, #0A65BF 100.0%);
-fx-text-fill: white;
}
.list-cell{
-fx-font-size:15.0;
}
.list-cell:even { /* <=== changed to even */
-fx-background-color: white;
}
.list-cell:filled:hover {
-fx-background-color: #0093ff;
-fx-text-fill: white;
}
How to use external css in JavaFX?
如何在 JavaFX 中使用外部 css?
How to use external css file to style a javafx application 如何使用外部 css 文件设置 javafx 应用程序的样式
How to programmatically change the size of text?:
如何以编程方式更改文本的大小?:
Before Java8:在 Java8 之前:
listView.setCellFactory(new Callback<ListView<String>, ListCell<String>>() {
@Override
public ListCell<String> call(ListView<String> p) {
return new ListCell<String>() {
@Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (item != null) {
setText(item);
// decide to add a new styleClass
// getStyleClass().add("costume style");
// decide the new font size
setFont(Font.font(16));
}
}
};
}
});
With lambda expressions:使用 lambda 表达式:
listView.setCellFactory(cell -> {
return new ListCell<String>() {
@Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (item != null) {
setText(item);
// decide to add a new styleClass
// getStyleClass().add("costume style");
// decide the new font size
setFont(Font.font(16));
}
}
};
});
Store text and text size in a item class在项目类中存储文本和文本大小
public class SizedText {
private final int textSize;
private final String text;
public SizedText(int textSize, String text) {
this.textSize = textSize;
this.text = text;
}
public int getTextSize() {
return textSize;
}
public String getText() {
return text;
}
}
And use a cellFactory
that returns ListCell
s that set the text size according to the item data in the updateItem
method.并使用
cellFactory
返回ListCell
s,根据updateItem
方法中的项目数据设置文本大小。
@Override
public void start(Stage primaryStage) {
ListView<SizedText> list = new ListView<>(FXCollections.observableArrayList(
new SizedText(10, "10"),
new SizedText(15, "15"),
new SizedText(20, "20"),
new SizedText(25, "25"),
new SizedText(30, "30"),
new SizedText(35, "35"),
new SizedText(40, "40"),
new SizedText(50, "50"),
new SizedText(60, "60")
));
list.setCellFactory(l -> new ListCell<SizedText>() {
@Override
protected void updateItem(SizedText item, boolean empty) {
super.updateItem(item, empty);
if (empty || item == null) {
setText(null);
} else {
setText(item.getText());
setFont(Font.font(item.getTextSize()));
}
}
});
Scene scene = new Scene(list);
primaryStage.setScene(scene);
primaryStage.show();
}
In javaFx you can use css to style different objects.在 javaFx 中,您可以使用 css 来设置不同对象的样式。 It depends on the object in your list view but it would go something like this.
这取决于列表视图中的对象,但它会像这样。
ListView<Hyperlink> listview = ..
ObservableList<Hyperlink> lists = listview.getItems();
for(Hyperlink link:lists)
{
link.setStyle("-fx-background-color:#ffffff");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.