1

我想创建一个 JavaFX 表,在一列的单元格中,允许用户编辑 XHTML 文本。我只需要非常基本的格式化功能,比如粗体、斜体、罢工线。

我已经设法通过使用我自己的 TableCell 子类并为每个单元格使用 WebView 来实现这一点(HTMLEditor 当然是另一种选择,但我的猜测是,对于我的要求,WebView 应该就足够了)。

但是,为了让用户编辑舒适,我需要以下功能: 1. 如果用户输入多行文本,则单元格高度需要调整大小。2. 上下文菜单(或者,如果不可能,其他菜单或按钮)应该允许格式化单元格中的部分文本,如上所述(粗体、斜体..)

有没有人成功实施类似的东西?我在网上看到过一些建议,但它们很少包含代码示例。

4

1 回答 1

0

I have succedded doing something similar.
I figured I can share some of the basic clues that allowed me to achieve it.

  1. Resize the whole WebView. For that, the whole WebView must be an editable html page. You achive that by setting contenteditable to true:

    <body contenteditable='true' id='content'></body>

  2. You can have a context menu over a webview. But it is something tricky, as you must first disable the original context menu associated to it.

    WebView editView;

    ...

    EventDispatcher originalDispatcher = editView.getEventDispatcher(); editView.setEventDispatcher(new WebmenuEventDispatcher(originalDispatcher));

And this is the event dispatcher class:

 public class WebmenuEventDispatcher implements EventDispatcher {

        private EventDispatcher originalDispatcher;

        public WebmenuEventDispatcher(EventDispatcher originalDispatcher) {
            this.originalDispatcher = originalDispatcher;
        }

        @Override
        public Event dispatchEvent(Event event, EventDispatchChain tail) {
            if (event instanceof MouseEvent) {
                MouseEvent mouseEvent = (MouseEvent) event;
                if (MouseButton.SECONDARY == mouseEvent.getButton()) {
                    mouseEvent.consume();
                    // Show our own menu
                    cmEdit.show(editView.getScene().getWindow(), mouseEvent.getScreenX(), mouseEvent.getScreenY());
                }
            }
            return originalDispatcher.dispatchEvent(event, tail);
        }
    }

Now, for setting the font from within that menu, you need a bidirectional Java<->javascript bridge and use some javascript in the webview side.

于 2012-06-23T11:36:49.183 回答