Skip to content

autoheight based on number of lines? #930

@3xau1o

Description

@3xau1o

any ideas on how to grow height RichTextFX height to match the number of lines?, i mean to autogrow the control hieght and avoid the vertical slider (with virtualizedScrollPane) to show and see all the text, like jupyter notebook code cells do.

size can be obtained with
Bounds bounds = codeArea.getLayoutBounds();
then to grow height
codeArea.setPrefSize(bounds.getWidth(), bounds.getHeight() + grow)

the question is to calculate the grow or the size based on the number of lines, or grow until vertical scrollbar disappears, but how do i know if the vertical slider is present?

UDPATE : This methods worked for me, not perfect but job is done, suggestions accepted

    public void autoresize() {
        double rows = getDocument().getParagraphs().size();
        Optional<Bounds> lineBound = getParagraphBoundsOnScreen(0);
        double rowSize_;

        if(rowSize==null){
            if (lineBound.isPresent() == true){
                rowSize_ = lineBound.get().getHeight();
                rowSize = rowSize_;
            }
            else rowSize_ = 25d;
        }
        else rowSize_ = rowSize;

        this.setPrefSize(getLayoutBounds().getWidth(), rowSize_ * rows + 5); //(rows*0.95d)
    }

    public void autoresizeAlternative() {
        Text text = new Text();
        text.setFont(new Font(18));
        text.setWrappingWidth(getWidth());
        text.setText(getText());
        double rows = getDocument().getParagraphs().size();
        double fix = (rows >= 20) ? rows - rows * 0.1d : 0d;
        setPrefSize(getLayoutBounds().getWidth(), text.getLayoutBounds().getHeight() + fix);
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions