JavaFX: How to make my custom component use all available space from the parent layout?

I'm trying to create a custom component for javafx, so I make my class extends javafx.scene.control.Control, and on the constructor it draws its contents.

My doubts: how can I make "grow" to fill all the space available on its parent layout? For example, when I put TextArea instead ...

This is a concise example of what I'm doing:

@Override public void start(Stage primarystage) throws Exception { BorderPane bpane = new BorderPane(); mainscene = new Scene(bpane, 800, 600); ((BorderPane) this.mainscene.getRoot()).setCenter(t); primarystage.setScene(mainscene); primarystage.show(); } 

In this example, TextArea will get all the space that the layout provides. I want my component to do the same!

All that I already tried to do: - Change the properties setMaxWidth / Height, setPrefWidth / Height, setMinWidth / Height; (when I set MinValues, it always uses these values ​​for drawing, but does not update the width value anymore) - Tried to use Double.MAX_VALUE to force a larger size and then draw layouts; Everything did not work :( I’m somehow mistaken ... - I also cried, but even this does nothing. (

Perhaps the key, I set the listener as shown below, and every time I resize my application / stage, it updates TextArea. But when I add this listener, my component never receives updates ...

 TextArea t = new TextArea("teste"); t.widthProperty().addListener(new ChangeListener() { public void changed(ObservableValue observable, Object oldValue, Object newValue) { System.out.println(oldValue + "|" + newValue); } }); 

Perhaps I am moving on to some properties or forgetting something, I don’t know ...

I already tried Google, but it seems the JavaFX documentation is too small and superficial, and many of them are based on a JavaFX script ...

I also agree with any tutorial that Oracle best offers ...

Thanks for any help ... I wrote a lot ... = O

+5
java javafx
source share
2 answers

I found that I was wrong ...

To make a component grow with its parent, all you have to do is define the maximum and / or maximum maximum double value (as shown below), and also let minwidth / height and prefwidth / height with its default value.

 this.setMaxWidth(Double.MAX_VALUE); this.setMaxHeight(Double.MAX_VALUE); 

With this configuration, when the parent changes the available space, it will use the final setWidth () / setHeight () method to update the width / height that your component can use. So, since these methods are final, you cannot override it to “know” when the change occurs. You will need to add a list of changes as follows to receive a notification when you need to repaint your component.

  t.widthProperty().addListener(new ChangeListener() { public void changed(ObservableValue observable, Object oldValue, Object newValue) { System.out.println(oldValue + "|" + newValue); } }); 

Hope this helps many others ...

+16
source share

I found (for Java 8) that user controls only correctly resize inside the parent node when they belong to GridPane, HBox, VBox or, preferably, to StackPane. The Oracle CustomControl example uses a VBox. I could not resize to work with AnchorPane as the top node of my custom control, even when maxWidth and maxHeight are explicitly set

So, the modified CustomControl.fxml looks like this:

 ?xml version="1.0" encoding="UTF-8"?> <!-- Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. --> <?import java.lang.*?> <?import javafx.collections.*?> <?import javafx.geometry.*?> <?import javafx.scene.control.*?> <?import javafx.scene.layout.*?> <fx:root type="javafx.scene.layout.StackPane" xmlns:fx="http://javafx.com/fxml"> (and here inside the StackPane one can place the actual control, eg the Anchor Pane with child nodes from ScreenBuilder - works without maxWidth or maxHeight) </fx:root> 

and CustomControl.java extends StackPane (instead of VBox)

+3
source share