我想创建一个包含数据项和 jCombobox 的列表。我使用这个 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;
}
}
当我运行应用程序时,一切正常,但是当我单击组合框时没有任何反应。
有人有想法吗?提前致谢。