我在LWUIT中使用ComboBox
。
我的代码是这样的。
public Locations(String name, int X, int Y)
{
Name = name;
xLocation = X;
yLocation = Y;
}
我列出了Locations
private List getLocations()
{
List list = new List();
list.addItem(new Locations("Landmark1", 23, 40));
list.addItem(new Locations("Landmark3", 24, 40));
list.addItem(new Locations("Landmark4", 25, 40));
list.addItem(new Locations("Landmark6", 26, 40));
return list;
}
然后使ComboBox包含List。
comboBox_Locations = new ComboBox(getLocations().getModel());
现在,我的问题是如何只显示ComboBox上的位置名称? 我知道我可以像这样列出名称的字符串列表:
private List getLocations()
{
List list = new List();
list.addItem(new Locations("Landmark1", 23, 40).Name);
list.addItem(new Locations("Landmark3", 24, 40).Name);
list.addItem(new Locations("Landmark4", 25, 40).Name);
list.addItem(new Locations("Landmark6", 26, 40).Name);
return list;
}
但是当我得到selectedItem
时,它只获取名称而我无法获得坐标。
我想做的就是得到这样的课程:
Object item = comboBox_Locations.getSelectedItem();
if (item.getClass() == Locations.class)
{
String Name = ((Locations)item).Name.toString();
int xCoords = ((Locations)item).getX();
int yCoords = ((Locations)item).getY();
}
这样我就可以使用xCoords和yCoords。
答案 0 :(得分:3)
使用渲染器:
list.setRenderer(new DefaultListCellRenderer() {
public Component getCellRendererComponent(Component list, Object model, Object value, int index, boolean isSelected) {
String t = ((Locations)value).getName();
return super.getCellRendererComponent(list, model, t, index, isSelected);
}
});
答案 1 :(得分:0)
好的,你可以尝试这种方式。从Locations
获取名称并将其添加到ListModel
。稍后您必须使用此ComboBox
作为参数创建ListModel
。
答案 2 :(得分:0)
创建自己的ListRenderer类。
import com.sun.lwuit.Component;
import com.sun.lwuit.Label;
import com.sun.lwuit.List;
import com.sun.lwuit.list.ListCellRenderer;
public class ListRenderer extends Label implements ListCellRenderer {
public ListRenderer() {
}
public Component getListCellRendererComponent(List value, Object obj,
int arg2, boolean isSelected) {
// TODO Auto-generated method stub
Locations listClassObj = (Locations) obj;
Label listValue = new Label(listClassObj.Name);
return listValue;
}
public Component getListFocusComponent(List arg0) {
// TODO Auto-generated method stub
setText("");
setFocus(false);
return this;
}
public void repaint() {
}
}
然后,另一种将字符串数组转换为List数组的方法
public ComboBox getList(String[] locations,int[] x,int[] y){
Locations[] locationObjs=new Locations[locations.length];
for(int i=0;i<locations.length;i++){
locationObjs=new Locations(locations[i],x[i],y[i]);
}
return new ComboBox(locationObjs);
}
private ComboBox getLocations(){
String[] locations={"landmark1","landmark2","landmark3","landmark4"};
int[] x={23,24,25,26};
int[] y={40,40,40};
ComboBox list=getList(locations,x,y);
return list;
}
然后,将其设置为组合框
combobox_locations=getLocations();
combobox_locations.setListCellRenderer(new ListRenderer());
然后使用,下面的代码
Object item = comboBox_Locations.getSelectedItem();
if (item.getClass() == Locations.class)
{
String Name = ((Locations)item).Name.toString();
int xCoords = ((Locations)item).getX();
int yCoords = ((Locations)item).getY();
}