3

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

2 Answers2

3

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.

jewelsea
  • 150,031
  • 14
  • 366
  • 406
  • Yes, great idea, I extended standard TableCell and set WebView as graphics (TableCell.setGraphics). That allowed me to display my HTML. Only problem I have now is: table cell is resized to extra big, even after set some maxWidth and maxHeight attributes to WebView. After adding simple Label all cells "wraps content", but with WebView cell has dimensions almost equals to Window. – Bartosz Kraszewski Mar 11 '14 at 21:42
  • Edit your question and update it to add your WebView cell factory code. – jewelsea Mar 11 '14 at 22:00
  • Updated answer to explain WebView sizing. – jewelsea Mar 11 '14 at 23:37
1

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