文本格式在单个表格单元格中不同 [英] Text Formatted Differently in a single Table Cell
问题描述
我想在TableView的单个单元格中对文本应用两种或三种不同的样式。
I would like to apply two or three different styles to text in a single cell in a TableView.
例如,我希望单个单元格的文本格式如下:
For example, I'd like the single cell to have text formatted like this:
太阳的边缘 [EP] (光盘2)
我真的很喜欢用颜色来做。
I'd really like to do it with colors, as well.
我知道如何将样式应用于整个单元格,但我甚至不知道从哪里开始将样式应用于部分文本。
I know how to apply styling to the entire cell, but I don't even know where to start for applying style to part of the text.
将数据放在不同的列中是不可行的选择。
Putting the data in different columns isn't a viable option.
推荐答案
下面是一个快速示例
- 使用 TextFlow 来设置文本的部分样式
- 实现了一个自定义的TableCell TextFlow作为其图形并根据需要更新文本部分
- uses TextFlow to style parts of a text
- implements a custom TableCell that has a TextFlow as its graphics and updates the text parts as appropriate
请注意,存在轻微的视觉故障:流的预测似乎是返回行的累积高度,就好像它们被包裹一样,即使它们没有被包裹,从而使行高度过大。作为一个快速入侵,computePrefHeight被覆盖以强制单行 - 缺点是如果列宽减小,则其他行/ s会消失。非常确定有更好的东西但是懒得进一步挖掘;)
Note that there is a slight visual glitch: the prefHeight of the flow seems to return the accumulated height of the lines as if they were wrapped even if they aren't, thus making the row height oversized. As a quick hack, the computePrefHeight is overridden to force a single line - with the drawback that the other line/s simply disappear if the column width is decreased. Pretty sure there's something better but too lazy to further dig ;)
import java.util.Locale;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.beans.property.SimpleObjectProperty;
import javafx.collections.FXCollections;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.Label;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.BorderPane;
import javafx.scene.text.TextFlow;
import javafx.stage.Stage;
/**
*
*/
public class TableFormattedCell extends Application {
public static class MyCell extends TableCell<Locale, Locale> {
private TextFlow flow;
private Label displayName;
private Label displayLanguage;
public MyCell() {
displayName = new Label();
displayName.setStyle("-fx-font-weight: bold");
displayLanguage = new Label();
displayLanguage.setStyle("-fx-font-style: italic; -fx-text-fill: darkviolet");
flow = new TextFlow(displayName, displayLanguage) {
@Override
protected double computePrefHeight(double width) {
// quick hack to force into single line ...
// there must be something better ..
return super.computePrefHeight(-1);
}
};
setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
setGraphic(flow);
}
@Override
protected void updateItem(Locale item, boolean empty) {
super.updateItem(item, empty);
if (empty || item == null) {
displayName.setText("");
displayLanguage.setText("");
} else {
displayName.setText(item.getDisplayName() + " ");
displayLanguage.setText(item.getDisplayLanguage());
}
}
}
private Parent getContent() {
TableView<Locale> table = new TableView<>(FXCollections.observableArrayList(
Locale.getAvailableLocales()));
TableColumn<Locale, String> countryCode = new TableColumn<>("CountryCode");
countryCode.setCellValueFactory(new PropertyValueFactory<>("country"));
TableColumn<Locale, String> language = new TableColumn<>("Language");
language.setCellValueFactory(new PropertyValueFactory<>("language"));
table.getColumns().addAll(countryCode, language);
TableColumn<Locale, Locale> local = new TableColumn<>("Locale");
local.setCellValueFactory(c -> new SimpleObjectProperty<>(c.getValue()));
local.setCellFactory(e -> new MyCell());
table.getColumns().addAll(local);
BorderPane pane = new BorderPane(table);
return pane;
}
@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setScene(new Scene(getContent(), 800, 400));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
@SuppressWarnings("unused")
private static final Logger LOG = Logger
.getLogger(TableFormattedCell.class.getName());
}
这篇关于文本格式在单个表格单元格中不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!