oskll
oskll

Reputation: 47

Increase the height of a textarea

I'm using JFoenix for my design components and I have a JFXTextArea which just act like a normal TextArea and I want it to adjust it's height following the text inside it, when I'm typing inside it and when it's full the text go to the line but a scrollbar appears, so I just want it to go to the line but instead of having a scrollbar I want the TextArea to be bigger to fit the text, is it possible ?

I'm using SceneBuilder for the user interface, I have a Controller and a fxml file for the components.

Here you can see screens of how it is and how I want it to be :

How it is

How I want to make it

If you want to know what JFoenix is you can take a look here : http://www.jfoenix.com/

Thank you

Upvotes: 0

Views: 184

Answers (1)

oskll
oskll

Reputation: 47

So I did it like this, I dont think it's the cleanest way to do it but it does the job so I put it here if someone else try to do the same thing and maybe have some advice about a better way to do it :

My TextArea has 557 as Y Layout, and 35 for the height as default

//This is when you are typing, if the scrollbar go visible when we increase the size of the TextArea
private void msgFieldFull(KeyEvent event)
{   
    ScrollBar scrollBarText = (ScrollBar) this.msgField.lookup(".scroll-bar:vertical");

    if(scrollBarText.isVisible()==true)
    {
        this.msgField.setLayoutY(this.msgField.getLayoutY()-10);
        this.msgField.setPrefHeight(this.msgField.getPrefHeight()+10);
    }

//If he is deleting somethings, the Y value can only be > 557 if the size increased so it won't get lower than 557
    if(event.getCode() == KeyCode.BACK_SPACE && this.msgField.getLayoutY() != 557)
    {

        this.msgField.setLayoutY(this.msgField.getLayoutY()+10);
        this.msgField.setPrefHeight(this.msgField.getPrefHeight()-10);
    }


}

//if he does CTRL+A and delete: default values for Y position and height
@FXML
private void msgFieldEmpty(KeyEvent event)
{
    if(this.msgField.getText().trim().isEmpty())
    {
        this.msgField.setLayoutY(557);
        this.msgField.setPrefHeight(35);
    }
}

Upvotes: 1

Related Questions