Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javaFX 2.0 set component to full width and height of immediate parent

Tags:

java

javafx-2

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

like image 970
Farouk Alhassan Avatar asked May 30 '11 09:05

Farouk Alhassan


People also ask

What is prefWidth in JavaFX?

prefWidth(double height) Called during layout to determine the preferred width for this node. DoubleProperty.

What does getChildren return in JavaFX?

getChildren. Gets the list of children of this Parent .

How do I change the size of TextField in JavaFX?

How do I change the size of TextField in JavaFX? One common way to do that is to place a label control immediately to the left of the text field. Label lblName = new Label("Name:"); lblName. setMinWidth(75); TextField txtName = new TextField(); txtName.

How do I resize a button in JavaFX?

Button SizeThe methods setMinWidth() and setMaxWidth() sets the minimum and maximum width the button should be allowed to have. The method setPrefWidth() sets the preferred width of the button. When there is space enough to display a button in its preferred width, JavaFX will do so.


1 Answers

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()...

like image 163
kylejmcintyre Avatar answered Sep 26 '22 02:09

kylejmcintyre