While this question may be dated, I encountered the same issue and was determined to find a solution. By carefully debugging the JavaFX code step by step, I identified the source of the additional space that was causing problems.
Within the ComboBoxListViewSkin
class, the width calculation for the ComboBox
is handled by default. The compute method in this class delegates the computation to its superclass and compares the results with the preferred width obtained from the listView
field.
/** {@inheritDoc} */
@Override protected double computePrefWidth(double height, double topInset, double rightInset, double bottomInset, double leftInset) {
double superPrefWidth = super.computePrefWidth(height, topInset, rightInset, bottomInset, leftInset);
double listViewWidth = listView.prefWidth(height);
double pw = Math.max(superPrefWidth, listViewWidth);
reconfigurePopup();
return pw;
}
Interestingly, the listView
field actually represents an Anonymous Classes
instance that extends ListView
, adding specific functionality for ComboBox
. One overridden method within this anonymous class,
protected double computePrefWidth(double height)
, contains a line responsible for the unwanted additional whitespace.
pw = Math.max(comboBox.getWidth(), skin.getMaxCellWidth(rowsToMeasure) + 30);
The addition of 30 to the widest cell's width is hardcoded, making it challenging to modify easily. This detail initially escaped my notice, leading to confusion during the debugging process.
Although one option would be to extend the ComboBoxListViewSkin
class and override the computePrefWidth
method, it seems more fitting for the default implementation of ComboBoxListViewSkin
to offer a straightforward way to adjust this extra whitespace. As a result, I've submitted an issue on GitHub addressing this concern.