JavaFX HMTLEditor doesn't react on 'return

2019-05-16 08:46发布

I was trying to do some experiments with JavaFX' HTMLEditor component. I used the following code(excerpt):

    fxPanel=new JFXPanel();
    Platform.runLater(new Runnable() {
        @Override
        public void run() {
            Group group = new Group();
            scene = new Scene(group);               
            fxPanel.setScene(scene);
            view = VBoxBuilder.create().build();

            group.getChildren().add(view);


            edit = HTMLEditorBuilder.create().build();
           // toolPane = TabPaneBuilder.create().minHeight(60d).build();
            //toolPane.getTabs().add(new Tab("Allgemein"));

            view.getChildren().add(edit);

        }
    });

    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            jPanel1.add(fxPanel);
        }
    });

It works fine so far with one important exception - i can't use the return key for a BR - it seems just to be ignored. There is no reaction on this key at all. As far as i could see, any other key works as expected.

标签: java javafx-2
3条回答
Anthone
2楼-- · 2019-05-16 09:06

I noticed that CTRL-M works where Enter doesn't. So I just worked around this by putting a KeyListener on the JFXPanel, changing the KeyChar from 10 to 13 and reposting the event to the System Event Queue. This may stop working as intended later on if the HTMLEditor starts responding to both ENTER and CTRL-M though.

fxPanel.addKeyListener(new KeyListener() {

    public void keyTyped(KeyEvent e) {
        if (e.getKeyChar() == 10) {
            e.setKeyChar((char) 13);
            Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(e);
        }
    }

    public void keyPressed(KeyEvent e) {}

    public void keyReleased(KeyEvent e) {}
});

Anyone have a better idea for now?

Edit: I found another way to get the desired effect is to install a custom KeyEventDispatcher on the current keyboard focus manager like so:

KeyboardFocusManager kfm = DefaultKeyboardFocusManager.getCurrentKeyboardFocusManager();
kfm.addKeyEventDispatcher(new KeyEventDispatcher() {
    @Override
    public boolean dispatchKeyEvent(KeyEvent e) {
        if (DefaultKeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner() == jfxPanel) {
            if (e.getID() == KeyEvent.KEY_TYPED && e.getKeyChar() == 10) {
                e.setKeyChar((char) 13);
            }
        }
        return false;
    }
});

This has the advantage of changing the original KeyEvent rather than posting a new one afterwards, so that if HTMLEditor were to start responding to Enter events we wouldn't be doubling up.

查看更多
Animai°情兽
3楼-- · 2019-05-16 09:09

I still see this problem with Oracle JDK 10. Peeking into the HTMLEditorSkin, there is a Command.INSERT_NEW_LINE, but it is not performed when pressing 'Enter'. In principle, there is API for executing a Command, and that could be invoked from a key event filter, but the API is private.

The following is a hack around this limitation. It "works", but it is of course a hack that might break with future updates of JavaFX:

HTMLEditor editor = /* .. somehow get the HTMLEditor .. */
editor.addEventFilter(KeyEvent.KEY_PRESSED, event ->
{
    if (event.getCode() == KeyCode.ENTER)
    {
        event.consume();
        final HTMLEditorSkin skin = (HTMLEditorSkin) htmlEditor.getSkin();
        try
        {
            // Use reflection to invoke the private method
            // executeCommand(Command.INSERT_NEW_LINE.getCommand(), null);
            final Method method = skin.getClass().getDeclaredMethod("executeCommand", String.class, String.class);
            method.setAccessible(true);
            method.invoke(skin, Command.INSERT_NEW_LINE.getCommand(), null);
        }
        catch (Throwable ex)
        {
            throw new RuntimeException("Cannot hack around ENTER", ex);
        }
    }
});
查看更多
ら.Afraid
4楼-- · 2019-05-16 09:17

I found out that it's an already known bug in JavaFX.

https://javafx-jira.kenai.com/browse/RT-33354
and
http://javafx-jira.kenai.com/browse/RT-20887

But FYI, it was resolved as "Won't Fix" for JavaFX 2.2. There is no problem in JavaFX 8.

查看更多
登录 后发表回答