【问题标题】:Displaying String of an Object in an ArrayList<Object> in a JComboBox在 JComboBox 的 ArrayList<Object> 中显示对象的字符串
【发布时间】:2015-01-26 21:30:47
【问题描述】:

所以我的代码遇到了问题。

首先,我有一个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[] 数组作为输入:

String[] objectStrings = { "Bookcase", "Chest", "Door", "Safe",
            "Comfortable chair" };

JComboBox objectList = new JComboBox(objectStrings);

现在我希望我的JComboBox 显示我的ArrayList&lt;RoomObjects&gt; 字符串的每个对象的名称("Door""Oil Lamp""Chest""Wall Carving"), 但我不知道该怎么做。有什么建议吗?

【问题讨论】:

    标签: java swing arraylist jcombobox


    【解决方案1】:

    制作

    cellRoomObjects = new Vector();
    

    重写 RoomObjects 的 toString() 方法以仅返回名称(或标题或任何字符串字段)

    创建 JComboBox(cellRoomObjects);

    【讨论】:

    • 我可以看到 2 个人对此表示赞同,而且看起来很简单 - 我是直接将它添加到方法中还是?
    • 我试着把 cellRoomObjects = new Vector();在一个不起作用的方法和一个同样不起作用的属性中,正如它所说:ArrayList 不能转换为 Vector
    【解决方案2】:

    我只是随意命名变量,你可以重命名,

    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;
        }
    
    
    }
    

    现在你的数组列表

    // 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);
                }
            });
        }
    }
    

    试试这个(做必要的导入)

    【讨论】:

    • 我尝试了上述方法,但我在以下行得到了一个 nullPointerException: DefaultComboBoxModel dcbm =new DefaultComboBoxModel(cellRoomObjects.toArray());
    • 我只是复制粘贴你的 JComboBox myBox=new JComboBox();和 DefaultComboBoxModel dcbm =new DefaultComboBoxModel(cellRoomObjects.toArray()); myBox.setModel(dcbm);并将 String toString 方法放在我的 RoomObjects 类中
    • Uhh .. 它目前是一个非常大且非常混乱的 gui 代码,有 800 行,但我可以尝试将其缩减为您需要在粘贴箱上看到的内容:S
    • 哦,我现在把它修好了 - 谢谢你的帮助,我只是很愚蠢 - 我实际上没有运行这个方法,它在我的数组列表中添加了东西,这就是它有一个空指针的原因例外 - 你对我帮助很大,谢谢:)
    【解决方案3】:

    您需要做的是获取每个字符串并将它们存储在一个数组中: 例如这样的:

    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++
    }
    

    【讨论】:

      猜你喜欢
      • 2014-08-02
      • 2016-09-16
      • 2016-07-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-18
      • 2013-12-24
      相关资源
      最近更新 更多