文本格式在单个表格单元格中不同

时间:2017-10-23 01:12:40

标签: javafx

我想在TableView的单个单元格中对文本应用两种或三种不同的样式。

例如,我希望单个单元格的文本格式如下:

太阳边缘 [EP] (光盘2)

我真的很想用颜色来做。

我知道如何将样式应用于整个单元格,但我甚至不知道从哪里开始将样式应用于部分文本。

将数据放在不同的列中不是一个可行的选择。

1 个答案:

答案 0 :(得分:1)

以下是

的简单示例
  • 使用TextFlow设置文本部分的样式
  • 实现了一个自定义TableCell,它具有TextFlow作为其图形,并根据需要更新文本部分

请注意,存在轻微的视觉故障:流量的预测似乎返回线的累积高度,就好像它们被包裹一样,即使它们不是,因此使行高度过大。作为一个快速入侵,computePrefHeight被覆盖以强制单行 - 缺点是如果列宽减小,其他行/ s就会消失。非常确定有更好的东西,但是懒得进一步挖掘;)

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());
}