0

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.

purring pigeon
  • 4,141
  • 5
  • 35
  • 68

2 Answers2

3

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

José Pereda
  • 44,311
  • 7
  • 104
  • 132
0

I found an answer that works - though I am not sure if this is the best way to handle it:

private void loadTextFlowWithNotes() {
    textFlow.getChildren().clear();
     notes.forEach(note -> {
        Text nameText = new Text("Taken By: " + note.takenBy);
        nameText.setFill(Color.CRIMSON);
        textFlow.getChildren().add(nameText);
        textFlow.getChildren().add(new Text(System.lineSeparator()));

        Text takenOn = new Text("Taken On: " + note.takenOn.format(DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT)));
        takenOn.setFill(Color.CRIMSON);
        textFlow.getChildren().add(takenOn);
        textFlow.getChildren().add(new Text(System.lineSeparator()));
        textFlow.getChildren().add(new Text(System.lineSeparator()));

        textFlow.getChildren().add(new Text(note.noteText));
        textFlow.getChildren().add(new Text(System.lineSeparator()));
        textFlow.getChildren().add(new Text(System.lineSeparator()));

        Line line = new Line();
        line.setStartX(0);
        line.endXProperty().bind(textFlow.widthProperty());
        line.setFill(Color.CRIMSON);
        textFlow.getChildren().add(line);
        textFlow.getChildren().add(new Text(System.lineSeparator()));
        textFlow.getChildren().add(new Text(System.lineSeparator()));
    });
}
purring pigeon
  • 4,141
  • 5
  • 35
  • 68