JavaFX HTMLEditor:如何实现ChangeListener?

时间:2015-08-08 14:44:24

标签: javafx changelistener

我制作了一个工具,可以用纯文本在HTMLEditor中写文本。文本在textarea中显示为HTML代码。我放了一个KeyEvent-Handler来解决这个问题,但是如果我之后用鼠标改变了一些东西,并不是每个都有变化。 我的问题:是否可以将一个ChangeListener实现到HTMLEditor,以便显示每个更改? 非常感谢!!

ublic class JavaFXHtmlEditor extends Application {

@Override
public void start(Stage primaryStage) {

    VBox root = new VBox();
    root.setSpacing(10);
    HBox up = new HBox();
    up.setPrefHeight(300);
    up.setPrefWidth(500);
    up.setAlignment(Pos.CENTER);
    HBox down = new HBox();
    down.setPrefHeight(200);
    up.setPrefWidth(400);
    down.setAlignment(Pos.CENTER);
    HTMLEditor htmlEditor = new HTMLEditor();
    TextArea textArea = new TextArea();
    textArea.setWrapText(true);
    up.getChildren().add(htmlEditor);
    down.getChildren().add(textArea);
    root.getChildren().addAll(up, down);

    htmlEditor.addEventHandler(KeyEvent.KEY_RELEASED, new EventHandler<KeyEvent>() {

        @Override
        public void handle(KeyEvent event) {
            textArea.setText(htmlEditor.getHtmlText());
        }
    });

    htmlEditor.addEventHandler(MouseEvent.MOUSE_RELEASED, new EventHandler<MouseEvent>() {

        @Override
        public void handle(MouseEvent event) {
            textArea.setText(htmlEditor.getHtmlText());
        }
    });

    Scene scene = new Scene(root, 600, 400);

    primaryStage.setTitle("Plain Text to HTML");
    primaryStage.setScene(scene);
    primaryStage.show();
}

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    launch(args);
}

}

1 个答案:

答案 0 :(得分:1)

我现在就这样解决了

htmlEditor.addEventHandler(InputEvent.ANY, new EventHandler<InputEvent>() {

        @Override
        public void handle(InputEvent event) {
            textArea.setText(htmlEditor.getHtmlText());
        }
    });