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

Text box like Telegram. Is it possible? #440

Closed
otopba opened this issue Feb 1, 2017 · 6 comments
Closed

Text box like Telegram. Is it possible? #440

otopba opened this issue Feb 1, 2017 · 6 comments

Comments

@otopba
Copy link

otopba commented Feb 1, 2017

Hi!
I can not figure out how to make so that the text field can expand and shrink to a certain size. Just as it does in the Telegram.
Thank you!

ezgif com-video-to-gif

@JordanMartinez
Copy link
Contributor

Something like this might work:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import org.fxmisc.flowless.VirtualizedScrollPane;
import org.fxmisc.richtext.InlineCssTextArea;

public class AreaHeight extends Application {

    @Override
    public void start(Stage primaryStage) {
        InlineCssTextArea area = new InlineCssTextArea();
        VirtualizedScrollPane<InlineCssTextArea> pane = new VirtualizedScrollPane<>(area);

        area.getParagraphs().sizeProperty().values()
                .map(size -> {
                    int linesToDisplay;
                    if (size > 5) {
                        linesToDisplay = 5;
                    } else if (size == 0) {
                        linesToDisplay = 1;
                    } else {
                        linesToDisplay = size;
                    }

                    double lineHeight = 14;

                    return lineHeight * linesToDisplay;
                })
                .subscribe(pane::setPrefHeight);

        primaryStage.setScene(new Scene(pane, 500, 500));
        primaryStage.show();
    }

}

@otopba
Copy link
Author

otopba commented Feb 2, 2017

@JordanMartinez Thank you!
I found another approach

  area.totalHeightEstimateProperty().addListener(new ChangeListener<Double>() {
            @Override
            public void changed(ObservableValue<? extends Double> observable, Double oldValue, Double newValue) {
                if (newValue != null && newValue != 0.0 && newValue < 200.0) {
                    pane.setPrefHeight(newValue);
                }
            }
        });

@JordanMartinez
Copy link
Contributor

@otopba Why not use a lambda instead of a ChangeListener?

@otopba
Copy link
Author

otopba commented Feb 3, 2017

@JordanMartinez It's not necessary. I just prefer this way =)

@JordanMartinez
Copy link
Contributor

@otopba Does this resolve your issue? If so, could you close it?

@otopba
Copy link
Author

otopba commented Feb 11, 2017

@JordanMartinez Yes. Thank you!

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

2 participants