Re: jcombobox custom renderer

Discussion in 'Java' started by Albert, Mar 26, 2009.

  1. Albert

    Albert Guest

    Mark a écrit :
    > I want to display some business objects (Sections in this case) in a
    > jcombobox rather than strings and I've been able to get the drop down
    > list to successfully display the necessary text like so:
    >
    > sectionComboBox.setRenderer(new DefaultListCellRenderer() {
    > public Component getListCellRendererComponent(JList list, Object value,
    > int index, boolean isSelected, boolean cellHasFocus) {
    > super.getListCellRendererComponent(list, value, index,
    > isSelected,
    >
    > cellHasFocus);
    >
    > if (value instanceof Section) {
    > Section s = (Section)value;
    > setText(s.getDescription());
    > }
    >
    > return this;
    > }
    > });
    >
    > but the text in the *editor* is displayed as
    > com.mms.sourceit.server.Section[id=1] (the value of Section.toString())
    > rather than Section.getDescription().
    >
    > Does anyone know what I need to do to get the jcombobox editor to
    > display the Section's description?


    My guess is that the editor part of the jcombobox is a jtextfield, so
    you don't have access to a Section in the renderer. You should detect
    the case of editor with index == -1, and then get the selectedItem from
    the combobox to get the section.
     
    Albert, Mar 26, 2009
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. RobE
    Replies:
    2
    Views:
    2,116
    =?ISO-8859-1?Q?J=F6rg_Marti?=
    Aug 12, 2003
  2. Bryan R. Meyer
    Replies:
    4
    Views:
    3,953
    Bryan R. Meyer
    Sep 6, 2004
  3. Branko Kaucic
    Replies:
    4
    Views:
    13,599
    Branko Kaucic
    Sep 13, 2005
  4. Replies:
    2
    Views:
    2,341
  5. Beaned
    Replies:
    0
    Views:
    506
    Beaned
    Aug 25, 2006
Loading...

Share This Page