setEditable(boolean)
determines if the JComboBox
allows text entry in addition to selecting a value via pull-down.
setEnabled(boolean)
determines if the JComboBox
is able to be interacted with at all. If it is not enabled, it is displayed as grayed out.
A JComboBox
can have any mix of these properties -
setEditable(true)
+ setEnabled(true)
= JComboBox
allows text input in addition to pull down values and user can interact with it.
setEditable(false)
+ setEnabled(true)
= JComboBox
only allows values from the pull down to be selected and user can interact with it.
setEditable(true)
+ setEnabled(false)
= JComboBox
allows text input in addition to pull down values but user cannot interact with it.
setEditable(false)
+ setEnabled(false)
= JComboBox
only allows values from the pull down to be selected and user cannot interact with it.
A situation where you may have a JComboBox
with setEnabled(false)
and setEditable(true)
would be where you want a JComboBox
that allows text input, but the form is in a state where the value of the JComboBox
isn't applicable. You would usually have some action that would call setEnabled(true)
on the JComboBox
once it does become applicable.
For example, if you have something like a student housing form, there may be a question on the form like 'Do you need a parking space?' with a JCheckbox
. There's a JComboBox
for the brand of car and a JTextFied
for the license plate number. You may have the JComboBox
pre-populated with common car brands - Ford, Chevy, Toyota, Honda, etc. - but decide you also want to allow it to be editable in case someone owns something like a Lamborghini (and is staying in student housing - yeah, right...). The value for car brand and license plate number aren't needed unless the user selects the JCheckBox
signifying that they need a parking space. You would add a listener to the JCheckBox
that would call setEnabled(true)
on the JComboBox
and JTextField
when it was selected, and setEnabled(false)
when it wasn't.