0

I would like to create a list containing items with data and a jCombobox. I use this listCellRenderer :

public class DeliveryListCellRenderer extends JPanel implements ListCellRenderer{

     JLabel[] lbl = new JLabel[2];  
     JComboBox combo;

  public DeliveryListCellRenderer()  
  {  
    setLayout(new GridLayout(0,2,15,0));  
    lbl[0] = new JLabel("",JLabel.RIGHT);  
    add(lbl[0]);  
    lbl[1] = new JLabel("",JLabel.LEFT);  
    add(lbl[1]);
    String[] timeZones = {"timeZone 1", "timeZone 2", "timeZone 3", "timeZone 4"};

    combo = new JComboBox(timeZones); 
    combo.setSelectedIndex(1);

    add(combo);
  }  
  public Component getListCellRendererComponent(JList list,Object value,  
                      int index,boolean isSelected,boolean cellHasFocus)  
  {  
    Delivery delivery = (Delivery)value;  
    lbl[0].setText("X : "+delivery.getNode().getX());  
    lbl[1].setText("Y : "+delivery.getNode().getY());
    if(isSelected) setBackground(Color.CYAN);  
    else setBackground(Color.WHITE);  
    return this;  
  }  
}

When I run the application, everything appears ok, but nothing happens when I click on the combobox.

Does anybody have an idea ? Thanks in advance.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Elodie
  • 108
  • 9

2 Answers2

3

When I run the application, everything appears ok, but nothing happens when I click on the combobox.

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
0

You need to map the content to display in the ComboBox with your Object.

I would advice the following: (T being the type of your Object).

    public class CustomComboBoxRenderer extends JLabel implements ListCellRenderer<T> {

    @Override
    public Component getListCellRendererComponent(JList<? extends T> list, T value, int index, boolean isSelected, boolean cellHasFocus) {

    if (isSelected) {
        setBackground(list.getSelectionBackground());
        setForeground(list.getSelectionForeground());
    }
    else {
        setBackground(list.getBackground());
        setForeground(list.getForeground());
    }
    if (index == -1) {
        setOpaque(false);
        setForeground(list.getForeground());
    }
    else {
        setOpaque(true);
    }
    setFont(list.getFont());

    if (value != null) {
        setText(value.getName());
    }

    return this;
    }
}

ComboBox creation:

    JComboBox<T> comboBox = new JComboBox<T>();
    comboBox.setRenderer(new CustomComboBoxRenderer ());
    add(comboBox);

Hope this helps.

Jérémie
  • 557
  • 4
  • 15
  • 1
    _your problem comes from the transparency management_ how would that relate to the problem stated: _everything appears ok, but nothing happens when I click on the combobox_ (or in other words: this answer is wrong) – kleopatra Nov 13 '12 at 15:35
  • @kleopatra: thank you for your feedback. However, this works by me. I agree the comment "problem comes from the transparency management" was not the best ever, but the following part of the code maps the String to be displayed in the ComboBox with the data Object: if (value != null) { setText(value.getName()); } – Jérémie Nov 14 '12 at 09:55
  • please read the question (which concededly isn't overly clear and its requirement not supported in a JList because a _renderer_ isn't part of the container hierarchy) carefully ... your answer is completely unrelated. – kleopatra Nov 14 '12 at 10:08