FilipK
FilipK

Reputation: 125

Disabling JavaFX ComboBox input

How do I disable input on my editable Combobox? (Well, actually JFoenix JFXCombobox but it's basically the same apart from it's appearance)

setEditable(false) would disable keyboard input on the Combobox but the list would still appear

setDisabled(true) would disable whole Combobox but I want user to be able to focus Combobox so he can copy it's contents if necessery.

Why do I want it like that? In my forms user must first click edit button to be able to change stuff.

Upvotes: 1

Views: 1234

Answers (1)

Felix
Felix

Reputation: 2386

Basically, the method ComboBox.setEditable adds/removes an Editor to the ComboBox which can be retrieved via ComboBox.getEditor()

To keep the TextField (to copy from) but disable user-input, simply set the editable flag on the underlying TextField:

private ComboBox<String> myComboBox;
[...]

myComboBox.setEditable(true);
myComboBox.getEditor().setEditable(false);

EDIT:

As @jewelsea said in a comment below, you can hide the list as soon as the user requests to open it:

myComboBox.setOnShown(event -> comboBox.hide());

I think it would be "cleaner" to disable the button which opens the dropdown but unfortunately I have not yet found a way to do that.

Upvotes: 3

Related Questions