I have a JComboBox
that contains three Items {"Personel", "Magasinier", "Fournisseur"}
.
I want this JComboBox
to display the value "Choisir une option :"
, which is a non-selectable value.
I tried this code after initComponents();
:
this.jComboBox1.setSelectedItem("Choisir une option :");
but it doesn't work.
How can I do that ?
You could override the selection code in your
JComboBox
model, with code such as the following SSCCE:This will display a combo box with the intial selection of "
- Select an Option -
". As soon as the user selects another option, it will not be possible to select the original option again.The easiest and fastest way in my opinion is to use a customized
ListCellRenderer
I stumbled upon this question and made some Changes to Duncan's answer. My solution looks like this:
When adding a place holder you create a anonymous object and overriding the toString method. Implementation could look like this:
and the creation of the JEComboBox:
Pros
Cons