javafx-8textflow

How to infer a page break in a TextFlow?


I am trying to create a NotePad screen in my application. Once notes are created they are locked and unable to be edited, however new pages can be added to a note.

I thought that using the TextFlow would be a great control for this. What I would like to do is the following:

**Note Taker Name**
**Date of Note**
Line of Note Text
Line of Note Text
Line of Note Text
Line of Note Text
------------------------------------------
**Note Taker Name**
**Date of Note**
Line of Note Text
Line of Note Text
Line of Note Text
Line of Note Text

I have attempted it this way:

String s1 = "line of text";

textFlow.getChildren().add(new Text(s1));
textFlow.getChildren().add(new Text(System.lineSeparator()));
textFlow.getChildren().add(new Separator(Orientation.HORIZONTAL));
textFlow.getChildren().add(new Text(System.lineSeparator()));
textFlow.getChildren().add(new Text(s1));

scrollPane.setFitToWidth(true);

This provides me pretty much what I want, except the separator is just a tiny line. I would like the line to cross the entire TextFlow and I wasn't really sure how to go about that.

Thanks.


Solution

  • There is already a JavaFX built in control for your needs: a Separator (javadoc).

    @Override
    public void start(Stage primaryStage) {
        TextFlow textFlow = new TextFlow();
        Text nameText = new Text("Taken By me ");
        nameText.setFill(Color.CRIMSON);
        textFlow.getChildren().add(nameText);
        textFlow.getChildren().add(new Text(System.lineSeparator()));
    
        Text takenOn = new Text("Taken On: " + DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT).format(LocalDateTime.now()));
        takenOn.setFill(Color.CRIMSON);
        textFlow.getChildren().add(takenOn);
        textFlow.getChildren().add(new Text(System.lineSeparator()));
    
        textFlow.getChildren().add(new Text("this is a note"));
        textFlow.getChildren().add(new Text(System.lineSeparator()));
    
        // Separator
        final Separator separator = new Separator(Orientation.HORIZONTAL);
        separator.prefWidthProperty().bind(textFlow.widthProperty());
        separator.setStyle("-fx-background-color: red;");
        textFlow.getChildren().add(separator);
    
        textFlow.getChildren().add(new Text(System.lineSeparator()));
        textFlow.getChildren().add(new Text("this is another note"));
    
        Scene scene = new Scene(textFlow, 300, 250);
    
        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }
    

    Separator