Java FX: TableView - display simple HTML


Question

I'd like to know, if it is possible to render simple HTML tags in JavaFX TableView (b, i, subscript, supscript). In my code snippet I used default cellValueFactory, but maybe someone could tell me if exists any cell factory which allow me to display html.

From code:

class Data{
    private String row = "<b> Sample data</b>"

    public String getRow(){
        return row;
}

TableView<Data> tableView = new TableView();
TableColumn<Data,String> column = new TableColumn("Sample Column");
column.setCellValueFactory(new PropertyValueFactory<Data, String>("row"));
tableView.getColumns().addAll(column);

I wish I could see Sample Data in my table in bold. Thanks in advance!

--UPDATE Code that allows me to see my HTML, but resizes table cell, WebView size is ignored and not wrapped tight

 private class HTMLCell extends TableCell<Component, Component> {

  @Override
  protected void updateItem(Component item, boolean empty) {
   super.updateItem(item, empty);
   if (!empty) {
    WebView webView = new WebView();
    webView.setMaxWidth(200);
    webView.setMaxHeight(50);
    WebEngine engine = webView.getEngine();
    // setGraphic(new Label("Test"));
    setGraphic(webView);
    String formula = item.getFormula();
    engine.loadContent(formula);

   }
  }
 }

   TableColumn<Component, Component> formulaColumn = new TableColumn<>("Formula");
  formulaColumn.setMinWidth(300);
  formulaColumn.setCellFactory(new Callback<TableColumn<Component, Component>, TableCell<Component, Component>>() {

   @Override
   public TableCell<Component, Component> call(TableColumn<Component, Component> param) {
    return new HTMLCell();
   }
  });

  formulaColumn.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Component, Component>, ObservableValue<Component>>() {

   @Override
   public ObservableValue<Component> call(CellDataFeatures<Component, Component> param) {
    return new SimpleObjectProperty<Component>(param.getValue());
   }
  });
1
3
3/11/2014 10:12:05 PM

Accepted Answer

HTML in a WebView in a TableCell

You have to make your own cell factory which returns a WebView node into which you load your HTML content.

On Correctly Sizing the WebView

In terms of establishing the preferred size of the WebView node, that is a little tricky. It would be simpler if RT-25005 Automatic preferred sizing of WebView were implemented.

I think the sample code from your question will work if you just replace the maxSize setting for the WebView with a webView.setPrefSize(prefX, prefY) call. You will just have to guess what the prefX and prefY values should be as I don't know a good way of determining programmatically.

I think your code should work by setting the max size for the WebView, but the WebView doesn't seem to respect the max size setting and just uses the pref size setting, which I think may be a bug in Java 8b129 (you could file that in the JavaFX Issue Tracker with a minimal, executable test case which reproduces it and a description of your test environment).

TextFlow Alternative

You might also consider using TextFlow component for representing your styled text. It is not HTML, but if all you want to do is some simple styling like making some text in the cell bold or italic, it might be a good option.

3
3/11/2014 11:35:11 PM

Use HTML tables in WebEngine/WebView rather than TableView. Table examples


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon