[英]java jcombobox inside a listCellRenderer
I would like to create a list containing items with data and a jCombobox. 我想创建一个包含数据项和jCombobox的列表。 I use this listCellRenderer :
我用这个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.
提前致谢。
When I run the application, everything appears ok, but nothing happens when I click on the combobox.
当我运行该应用程序时,一切看起来都很好,但是当我单击组合框时什么也没有发生。
this isn't job for Renderer
( When I run the application, everything appears ok
), have to use XxxEditor
对于
Renderer
这不是工作( When I run the application, everything appears ok
),必须使用XxxEditor
basically this isn't possible for JList
, have look at JTable
基本上这对于
JList
是不可能的,请看一下JTable
JTable with one Column and/or without JTableHeader 具有一列和/或不具有JTableHeader的JTable
You need to map the content to display in the ComboBox with your Object. 您需要将内容映射为与对象一起显示在ComboBox中。
I would advice the following: (T being the type of your Object). 我建议如下:(T是您的对象的类型)。
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: ComboBox的创建:
JComboBox<T> comboBox = new JComboBox<T>();
comboBox.setRenderer(new CustomComboBoxRenderer ());
add(comboBox);
Hope this helps. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.