Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaFX - How to make ComboBox hgrow?


I have a problem with JavaFX(8), HBox, ComboBox and HGrow. HGrow does not work in combination with ComboBox.

(INFO: with TextField (instead of ComboBox), it works as expected!)

This is my FXML-Code:

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<VBox prefHeight="117.0" prefWidth="285.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="de.test.TestController">
  <children>
     <HBox prefHeight="105.0" prefWidth="196.0" VBox.vgrow="ALWAYS">
     <children>
        <ComboBox fx:id="fxCboTest" prefHeight="25.0" prefWidth="62.0" HBox.hgrow="ALWAYS" />
     </children>
    </HBox>
  </children>
</VBox>

this Code will result in:

enter image description here

i also tried following code (without success, this code does nothing):

HBox.setHgrow(uiController.fxCboTest, Priority.ALWAYS);

Does anyone has an idea how to make an ComboBox HGrow?

like image 330
Ben Avatar asked Apr 07 '15 10:04

Ben


People also ask

What is the difference between ComboBox and ChoiceBox JavaFX?

The JavaFX ComboBox control enables users to choose an option from a predefined list of choices, or type in another value if none of the predefined choices matches what the user want to select. The JavaFX ChoiceBox control enables users to choose an option from a predefined list of choices only.


2 Answers

This is an answer to my own question.
After some testing, I found out that when setting Max Width to MAX_VALUE, it works:

enter image description here

This will result in following code/xml from SceneBuilder:

...
<children>
   <ComboBox maxWidth="1.7976931348623157E308" prefWidth="150.0" HBox.hgrow="ALWAYS" />
</children>
...

where 1.7976931348623157E308 looks like Double.MAX_VALUE.

This will also work with multiple controls in Hbox.
enter image description here

In my opinion, this is not very consequently/consistently.
I still don't unserstand why HGrow does not work for ComboBox.

like image 83
Ben Avatar answered Sep 18 '22 07:09

Ben


This is a hack, but it should work. Inside the controller's intialize method, define a binding.

@Override
public void initialize(URL location, ResourceBundle resources) {
    fxCboTest.prefWidthProperty().bind(hbox.widthProperty());
}
like image 29
ItachiUchiha Avatar answered Sep 18 '22 07:09

ItachiUchiha