Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add setText method and make textProperty as not readonly StringProperty #1251

Open
PavelTurk opened this issue Nov 11, 2024 · 0 comments
Open

Comments

@PavelTurk
Copy link
Contributor

JavaFX TextArea contains two methods setText and appendText. Test code:

public class JavaFxTest10 extends Application {

    @Override
    public void start(Stage primaryStage) {
        var t = "Some text.\nSome text.\nSome text.\nSome text.\nSome text.\nSome text.\nSome text.\nSome text.\nSome text.";
        var textArea = new TextArea();
        var setButton = new Button("Set");
        setButton.setOnAction(e -> textArea.setText(t));
        var appendButton = new Button("Append");
        appendButton.setOnAction(e -> textArea.appendText(t));
        var clearButton = new Button("Clear");
        clearButton.setOnAction(e -> textArea.clear());
        VBox vbox = new VBox(textArea, new HBox(setButton, appendButton, clearButton));
        Scene scene = new Scene(vbox, 300, 100);
        primaryStage.setScene(scene);
        primaryStage.setTitle("TableView Binding Test");//yes, that's wrong title
        primaryStage.show();

    }

    public static void main(String[] args) {
        launch(args);
    }
}

Result:
Peek 2024-11-11 11-26

Currently RichTextFX areas don't have setText method, that sets new text and doesn't scroll to bottom, that's why it is necessary to use something like this:

public static void setText(InlineCssTextArea textArea, String text) {
        textArea.clear();
        textArea.appendText("\n");
        textArea.selectRange(0, 0);
        var changes = textArea.createMultiChange(2);
        changes.insertTextAbsolutely(0, 1, text);
        changes.deleteTextAbsolutely(0, 1);
        changes.commit();
    }

or to use custom class that extends InlineCssTextArea. I suggest to provide this feature out of box.

The second feature is related to set text. I suggest to make textProperty as a normal not readonly StringProperty. For example, this problem is well visible when MVVM pattern is used. When in ViewModel we need to get current value, process it and replace with new value. It should be this way

public class ViewModel {
    private final StringProperty text = new SimpleStringProperty();
   ...
    public void doIt() {
        var text = text.get();
        text = doSomething(text);
        text.set(text);
    }
}

and

public class View {
    private final InlineCssTextArea textArea = new InlineCssTextArea();
    ....
    this.textArea.textProperty().bindBidirectional(viewModel.textProperty());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant