java - display a non-selectable default value for JComboBox -
i have jcombobox
contains 3 items {"personel", "magasinier", "fournisseur"}
.
i want jcombobox
display value "choisir une option :"
, non-selectable value.
i tried code after initcomponents();
:
this.jcombobox1.setselecteditem("choisir une option :");
but doesn't work.
how can ?
you override selection code in jcombobox
model, code such following sscce:
public class jcomboexample { private static jframe frame = new jframe(); private static final string not_selectable_option = " - select option - "; private static final string normal_option = "normal option"; public static void main(string[] args) throws exception { jcombobox<string> combobox = new jcombobox<string>(); combobox.setmodel(new defaultcomboboxmodel<string>() { private static final long serialversionuid = 1l; boolean selectionallowed = true; @override public void setselecteditem(object anobject) { if (!not_selectable_option.equals(anobject)) { super.setselecteditem(anobject); } else if (selectionallowed) { // allow once selectionallowed = false; super.setselecteditem(anobject); } } }); combobox.additem(not_selectable_option); combobox.additem(normal_option); frame.add(combobox); frame.pack(); frame.setdefaultcloseoperation(jframe.dispose_on_close); swingutilities.invokelater(new runnable() { @override public void run() { frame.setvisible(true); } }); } }
this display combo box intial selection of "- select option -
". user selects option, not possible select original option again.
Comments
Post a Comment