【问题标题】:JavaFX HTMLEditor: how to implement a ChangeListener?JavaFX HTMLEditor:如何实现 ChangeListener?
【发布时间】:2015-08-08 14:44:23
【问题描述】:

我制作了一个工具,可以在 HTMLEditor 中以纯文本形式编写文本。文本在文本区域中显示为 HTML 代码。我放置了一个 KeyEvent-Handler 来解决这个问题,但是如果我之后用鼠标更改某些内容,则不会指示所有更改。 我的问题:是否可以为 HTMLEditor 实现 ChangeListener,以便显示每个更改? 非常感谢!!

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

}

【问题讨论】:

    标签: javafx changelistener


    【解决方案1】:

    我现在是这样解决的

    htmlEditor.addEventHandler(InputEvent.ANY, new EventHandler<InputEvent>() {
    
            @Override
            public void handle(InputEvent event) {
                textArea.setText(htmlEditor.getHtmlText());
            }
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-18
      • 2017-05-10
      • 1970-01-01
      • 2016-08-18
      • 2014-06-16
      • 2017-12-24
      • 1970-01-01
      相关资源
      最近更新 更多