19

How can I make a TextArea take the full width and height of the parent pane.

I tried this:

TextArea textArea = new TextArea();
textArea.setScaleX( 100 );
textArea.setScaleY( 100 );

but the element defined in the top via parent.setTop(...) was covered.
Reducing the scaleY had no effect.

What else do I have to do to achieve this?

Thanks

sk22
  • 837
  • 1
  • 10
  • 21
Farouk Alhassan
  • 3,780
  • 9
  • 51
  • 74

5 Answers5

34

The MAX_VALUE solution is a bit hacky and could cause performance issues. Also, the answer to this could depend on what your parent container is. Anyway, a better way to do it would be like this:

textArea.prefWidthProperty().bind(<parentControl>.prefWidthProperty());
textArea.prefHeightProperty().bind(<parentConrol>.prefHeightProperty());

You may also want to bind the preferred properties to the actual properties, especially if the parent is using it's computed dimensions rather than explicit ones:

textArea.prefWidthProperty().bind(<parentControl>.widthProperty());
textArea.prefHeightProperty().bind(<parentConrol>.heightProperty());

It's also possible to do this without using binding by overriding the layoutChildren() method of the parent container and calling

textArea.resize(getWidth(), getHeight());

Don't forget to call super.layoutChildren()...

Michael
  • 41,989
  • 11
  • 82
  • 128
kylejmcintyre
  • 1,898
  • 2
  • 17
  • 19
17

solved with this

textArea.setPrefSize( Double.MAX_VALUE, Double.MAX_VALUE );
Farouk Alhassan
  • 3,780
  • 9
  • 51
  • 74
5

You achieve this by placing the TextArea in a BorderPane.

Stage stage = new Stage();
stage.setTitle("Resizing TextArea");

final BorderPane border = new BorderPane();
Scene scene = new Scene(border);

TextArea textArea = new TextArea();
textArea.setStyle("-fx-background-color: #aabbcc;");

border.setCenter(textArea);

primaryStage.setScene(scene);
primaryStage.setVisible(true);

You can also place it inside an HBox or a VBox. Then resizing is limited to horizontal/vertical direction. Not sure if this is an issue.

pmoule
  • 4,322
  • 2
  • 34
  • 39
-2
<TextArea style="-fx-pref-height: 10px;"/>
Jeeva
  • 1,029
  • 3
  • 15
  • 21
-4

you can do that with creating a css file.

textarea
{
width://your width
} 
Mahdi_Nine
  • 14,205
  • 26
  • 82
  • 117