Java 在ArrayList中显示对象的字符串<;对象>;在JComboBox中
所以我的代码遇到了问题 首先,我有一个添加了对象的Java 在ArrayList中显示对象的字符串<;对象>;在JComboBox中,java,swing,arraylist,jcombobox,Java,Swing,Arraylist,Jcombobox,所以我的代码遇到了问题 首先,我有一个添加了对象的ArrayList: cellRoomObjects = new ArrayList<>(); RoomObjects door = new RoomObjects("Door" , 1 , true); RoomObjects oilLamp = new RoomObjects("Oil lamp", 2 , true); RoomObjects chest = new RoomObjects("Chest" , 3 , true)
ArrayList
:
cellRoomObjects = new ArrayList<>();
RoomObjects door = new RoomObjects("Door" , 1 , true);
RoomObjects oilLamp = new RoomObjects("Oil lamp", 2 , true);
RoomObjects chest = new RoomObjects("Chest" , 3 , true);
RoomObjects wallCarving = new RoomObjects("Wall Carving", 4 , false);
cellRoomObjects.add(door);
cellRoomObjects.add(oilLamp);
cellRoomObjects.add(chest);
cellRoomObjects.add(wallCarving);
现在我想让我的JComboBox
显示我的数组列表的字符串中每个对象的名称(“门”
,“油灯”
,“箱子”
和“墙壁雕刻”
),
但我不知道该怎么做。有什么建议吗?您需要做的是获取每个字符串并将其存储在一个数组中:
例如,类似这样的事情:
String[] myNames=new String[cellRoomObjects.size()];
int i= 0;
for(RoomObjects r : cellRoomObjects ){
myName[i]=r.getName() //assuming there is a method that gets name in RoomObjects that returns the string u want
i++
}
使
cellRoomObjects = new Vector();
重写RoomObjects的toString()方法以仅返回名称(或标题或字符串字段中的任何内容)
创建JComboBox(cellRoomObjects) 我只是随意命名变量,你可以重命名
public class CellRoomObject {
String itemName;
int itemNumber;
boolean ok;
public CellRoomObject(String itemName, int itemNumber, boolean ok) {
this.itemName = itemName;
this.itemNumber = itemNumber;
this.ok = ok;
}
@Override
public String toString() {
return this.itemName;
}
}
现在你的ArrayList
// create a JCOMBOBOx named say myCombobox
公共类MyFrame扩展了JFrame{
public MyFrame() {
setTitle("example");
setSize(300, 200);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JComboBox myBox=new JComboBox();
ArrayList<CellRoomObject> cellRoomObjects =new ArrayList<CellRoomObject>();
CellRoomObject c1=new CellRoomObject("Door" , 1 , true);
CellRoomObject c2=new CellRoomObject("Oil lamp", 2 , true);
CellRoomObject c3=new CellRoomObject("Chest" , 3 , true);
cellRoomObjects.add(c1);
cellRoomObjects.add(c2);
cellRoomObjects.add(c3);
DefaultComboBoxModel dcbm =new DefaultComboBoxModel(cellRoomObjects.toArray());
myBox.setModel(dcbm);
this.add(myBox);
setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
MyFrame ex = new MyFrame();
ex.setVisible(true);
}
});
}
}
publicmyframe(){
setTitle(“示例”);
设置大小(300200);
setLocationRelativeTo(空);
setDefaultCloseOperation(关闭时退出);
JComboBox myBox=新的JComboBox();
ArrayList cellRoomObjects=新的ArrayList();
CellRoomObject c1=新的CellRoomObject(“门”,1,真);
CellRoomObject c2=新的CellRoomObject(“油灯”,2,真);
CellRoomObject c3=新的CellRoomObject(“胸部”,3,真);
cellRoomObjects.add(c1);
cellRoomObjects.add(c2);
cellRoomObjects.add(c3);
DefaultComboxModel dcbm=新的DefaultComboxModel(cellRoomObjects.toArray());
myBox.setModel(dcbm);
添加(myBox);
setVisible(真);
}
公共静态void main(字符串[]args){
invokeLater(新的Runnable(){
@凌驾
公开募捐{
MyFrame ex=新的MyFrame();
例如,setVisible(真);
}
});
}
}
尝试此操作(执行必要的导入)我尝试了上述操作,但在以下行中得到了nullPointerException:DefaultComboxModel dcbm=new DefaultComboxModel(cellRoomObjects.toArray());我只是复制粘贴的ur JComboBox myBox=new JComboBox();和DefaultComboxModel dcbm=新的DefaultComboxModel(cellRoomObjects.toArray());myBox.setModel(dcbm);并将String-toString方法放在我的RoomObjects类中。。这是一个非常大,非常混乱的gui代码,目前有800行,但我可以尝试将其减少到你需要在粘贴箱上看到的内容:SOh我现在已经修好了-谢谢你的帮助,我只是太傻了-我实际上没有运行该方法,这会在我的arraylist中添加内容,这就是它出现空指针异常的原因-你帮了我很大的忙,谢谢:)我可以看到有两个人对此投了赞成票,这看起来很简单-我只是把它添加到方法中还是?我试着把cellRoomObjects=new Vector();无论是在一个不起作用的方法中,还是在一个attributem中,wich都不起作用,正如它所说:ArrayList不能转换为Vector
public MyFrame() {
setTitle("example");
setSize(300, 200);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JComboBox myBox=new JComboBox();
ArrayList<CellRoomObject> cellRoomObjects =new ArrayList<CellRoomObject>();
CellRoomObject c1=new CellRoomObject("Door" , 1 , true);
CellRoomObject c2=new CellRoomObject("Oil lamp", 2 , true);
CellRoomObject c3=new CellRoomObject("Chest" , 3 , true);
cellRoomObjects.add(c1);
cellRoomObjects.add(c2);
cellRoomObjects.add(c3);
DefaultComboBoxModel dcbm =new DefaultComboBoxModel(cellRoomObjects.toArray());
myBox.setModel(dcbm);
this.add(myBox);
setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
MyFrame ex = new MyFrame();
ex.setVisible(true);
}
});
}
}