Java FX: TableView - display simple HTML

时光毁灭记忆、已成空白 提交于 2019-12-18 09:00:41

问题


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:


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.




回答2:


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



来源:https://stackoverflow.com/questions/22334983/java-fx-tableview-display-simple-html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!