2 Replies Latest reply: May 4, 2012 5:16 AM by 934068 RSS

    Making HtmlEditor ReadOnly

    934068
      Hi,

      I am using JavaFx 2.1 and my requirement is to make the editor read-only. I tried disabling it, but if the text is to large the scroll bar is also disabled !
      I have some html tags in the text hence using the HTMLEditor.


      Thanks.
        • 1. Re: Making HtmlEditor ReadOnly
          jsmith
          How about something like this with swaps between an editor when in editing mode and view when in viewing mode.
          import javafx.application.Application;
          import javafx.event.ActionEvent;
          import javafx.event.EventHandler;
          import javafx.scene.Scene;
          import javafx.scene.control.ToggleButton;
          import javafx.scene.layout.StackPane;
          import javafx.scene.layout.VBox;
          import javafx.scene.web.HTMLEditor;
          import javafx.scene.web.WebView;
          import javafx.stage.Stage;
          
          public class ReadOnlyHtmlEditor extends Application {
            public static void main(String[] args) { launch(args); }
            @Override public void start(Stage primaryStage) {
              // create an html editor in which to edit text and a view in which to view it.
              final HTMLEditor editor = new HTMLEditor();
              editor.setHtmlText("<h1>The Gettysburg Address</h1><i>Four score and twenty years ago . . .</i>");
              final WebView view = new WebView();
              
              // create a stack which swaps back an forth between the html editor and view
              // depending on the state of the editToggleButton.
              final StackPane webStack = new StackPane();
              final ToggleButton editToggleButton = new ToggleButton("Edit");
              editToggleButton.setOnAction(new EventHandler<ActionEvent>() {
                @Override public void handle(ActionEvent arg0) {
                  webStack.getChildren().clear();
                  if (editToggleButton.isSelected()) {
                    webStack.getChildren().addAll(view, editor);
                  } else {
                    view.getEngine().loadContent(editor.getHtmlText());
                    webStack.getChildren().addAll(editor, view);
                  }
                }
              });
          
              // set the initial state of the scene to editing.
              editToggleButton.fire(); 
              
              // layout the scene.
              final VBox layout = new VBox(10);
              layout.setStyle("-fx-padding: 10; -fx-background-color: cornsilk");
              layout.getChildren().addAll(editToggleButton, webStack);
              primaryStage.setScene(new Scene(layout));
              primaryStage.show();
            }
          }
          • 2. Re: Making HtmlEditor ReadOnly
            934068
            thanks