12


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?

Ben
  • 3,378
  • 30
  • 46

3 Answers3

32

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.

Ben
  • 3,378
  • 30
  • 46
  • `hgrow` will not override a resizable node's maximum width, which is set to the preferred size by default. There's a little information on this in the [tutorial](http://docs.oracle.com/javase/8/javafx/layout-tutorial/size_align.htm#JFXLY133) and also an excellent [presentation at Parleys](https://www.parleys.com/tutorial/interface-layout-javafx-2-0) (registration required). – James_D Apr 07 '15 at 13:00
  • 1
    So, why does it work when just replacing ComboBox with TextField in XML? – Ben Apr 07 '15 at 13:07
  • 4
    Dug a little further into the default settings. The `maxWidth` defaults for both to the sentinel value `Region.COMPUTED_SIZE`, which causes a call to `computeMaxWidth(...)`. For controls, this in turn delegates to the skin implementation. Digging into the source code, `ComboBoxBaseSkin` resolves this by returning the preferred width; the `TextFieldSkin` just inherits the default `SkinBase` implementation, which returns `Double.MAX_VALUE`. Hence a combo box by default is limited to its preferred size; a text field is allowed to grow indefinitely. The bottom line is they have different defaults. – James_D Apr 08 '15 at 02:15
  • in JavaFX 8 you can use `maxWidth="Infinity"`, which looks better in the fxml. – tomorrow Jun 07 '19 at 12:57
2

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());
}
ItachiUchiha
  • 36,135
  • 10
  • 122
  • 176
  • Thanks for your suggestion, but this code may not work properly when (not in my code example, cause i want keep my example as simple as possible) the hbox contains multiple controls, like ComboBox and Button. i want to avoid too much calculating by myself. – Ben Apr 07 '15 at 11:45
  • The question never mentioned that you wanted multiple children in the HBox. – ItachiUchiha Apr 07 '15 at 12:47
  • You are right, this is why i add an comment to your answer why your solution **may** not work with multiple controls. i didn't said that your solution is absolute unhelpful. – Ben Apr 07 '15 at 13:01
2

I had the same problem, but I'm not using Scene Builder.
So my fix, based on Ben's answer, is in the code below:

ComboBox comboBox = new ComboBox(...);
...
comboBox.setMaxWidth(Double.MAX_VALUE);
סטנלי גרונן
  • 2,917
  • 23
  • 46
  • 68
pinkston00
  • 168
  • 9