1

I have JavaFX controls that can be associated with many, potentially conflicting values.

When the control is a TextField, I use the promptText property to put the string (Multiple conflicting values) into the TextField and use the trick described here to ensure the promptText remains visible until the user types into the field. This works great.

Now, I would like to do the same thing with a ComboBox, but I have discovered that a ComboBox will display promptText only if the editable property is set to true. This is a problem for me, because I do not want the user to type arbitrary strings into the ComboBox. I want to limit him to a set of predetermined selections.

Is there a way to make ComboBox display the promptText even if its editable property is set to false? Extending ComboBox is an acceptable solution in my case.

Community
  • 1
  • 1
davidrmcharles
  • 1,923
  • 2
  • 20
  • 33

1 Answers1

2

Everything seems to work if I set the ComboBox editable, and then acquire the editor (a TextField) and set it to not-editable.

comboBox.setEditable(true)
comboBox.getEditor().setEditable(false)

To keep the promptText visible until the user changes the ComboBox, one must apply the CSS tricks to the interior TextField, not the ComboBox itself.

Unsolved Problems:

  • The user cannot type into the ComboBox, but it looks like he can: it is styled like he can, and the mouse cursor even assumes the I-beam appearance when it is hovering over the editor portion of the ComboBox.

  • The user must click on the button to unroll and see the items. He can no longer click on the larger target area of a non-editable ComboBox.

  • I have seen a loss of focus cause such a ComboBox to generate a selectedItem changed event, and the new value is not the object originally added to ComboBox.getItems(), but a string representation of the original object! It seems that selectedIndex() can be used to work around this.

  • Last and least: my ComboBox's StringConverter started receiving nulls when previously, it did not.

davidrmcharles
  • 1,923
  • 2
  • 20
  • 33