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:

  1. your jcombobox items must object example:

    public class item {      private string number;      private string name;      // constructor + setters , getters  } 
  2. a listcellrenderer customize how render values in popup list or in text-field of jcombobox:

        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

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -