Java jcombobox different text in its dropdown and different text in its textfield -
i want create jcombobox have following appearance behaivour:
1) in dropdown list each line should code number , item name.
2) when user selects 1 of lines, in textfield component of combobox code number should appear , not item name. (something this)
how can that?
thanks in advance.
it not hard using 2 steps:
your
jcombobox
items must object example:public class item { private string number; private string name; // constructor + setters , getters }
a
listcellrenderer
customize how render values in popup list or in text-field ofjcombobox
:jcombobox<item> jc = new jcombobox<item>(); jc.setrenderer(new listcellrenderer<item>() { @override public component getlistcellrenderercomponent( jlist<? extends item> list, item value, int index, boolean isselected, boolean cellhasfocus) { if(isselected && list.getselectedindex () != index) return new jlabel(value.getnumber()); return new jlabel(value.getnumber() +" "+value.getname()); } });
good luck.
Comments
Post a Comment