Question: how to make an expandable combobox? So that when expanding the window with the mouse, ComboBox also expanded. Required to be sure to the left of the ComboBox was a Label.

Code: (fxml)

<?import javafx.scene.control.ComboBox?> <?import javafx.scene.control.Label?> <?import javafx.scene.layout.*?> <HBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"> <children> <AnchorPane> <children> <Label text="Label" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" /> </children> </AnchorPane> <AnchorPane> <children> <ComboBox AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" /> </children> </AnchorPane> </children> </HBox> 

    2 answers 2

    AnchorPane for such tasks is not suitable. You need to use another container, for example, BorderPane .

    Example fxml:

     <?xml version="1.0" encoding="UTF-8"?> <?import javafx.scene.control.*?> <?import java.lang.*?> <?import javafx.scene.layout.*?> <BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"> <left> <Label text="Label" BorderPane.alignment="CENTER" /> </left> <center> <ComboBox maxWidth="1.7976931348623157E308" prefWidth="150.0" BorderPane.alignment="CENTER" /> </center> </BorderPane> 
    • Thanks, I found the answer through HBox, I think BorderPane will be cumbersome, although it is interesting that BorderPane has this behavior by default. - Log1c
     <?xml version="1.0" encoding="UTF-8"?> <?import javafx.scene.control.ComboBox?> <?import javafx.scene.control.Label?> <?import javafx.scene.layout.*?> <HBox xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"> <children> <AnchorPane HBox.hgrow="NEVER"> <children> <Label text="Label" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" /> </children> </AnchorPane> <AnchorPane HBox.hgrow="SOMETIMES"> <children> <ComboBox AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" /> </children> </AnchorPane> </children> </HBox>