使用 JList 显示 TreeMap 对象

发布于 2024-12-11 22:27:41 字数 1584 浏览 0 评论 0原文

我想遍历 TreeMap 将对象添加到 DefaultListModel,然后添加到 JList。但是,列表中仅显示键值,而不显示对象。我该如何纠正这个问题?

请参见下面的代码:

public class ViewInventoryInterface extends JFrame {
private Inventory theInventory; // reference to back end
private InventoryUPCIterator iter;
private DefaultListModel dlm;
private JList list;
private JScrollPane scroll;

public ViewInventoryInterface(Inventory theInventory) {
    this.theInventory = theInventory;
    iter = theInventory.inventoryUPCIterator(); //returns an iterator for the inventory
    dlm = new DefaultListModel();
    while (iter.hasNext()) {
        dlm.addElement(iter.next());
    }
    list = new JList(dlm);
    scroll = new JScrollPane(list);
    setTitle("Inventory");
    add(scroll);
    setSize(400, 400);
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    setLocationRelativeTo(null);
    setVisible(true);

}
}

这是创建迭代器的类

public class InventoryUPCIterator {

private Set<String> keys;
private Iterator<String> iter;

public InventoryUPCIterator(HashMap<String, ItemIndex> inventory) {
    TreeMap<String, ItemIndex> values = new TreeMap<String, ItemIndex>(
            inventory);
    // sorts the index according to the natural ordering of String values
    keys = values.keySet();// returns set of sorted keys
    iter = keys.iterator();
}

public boolean hasNext() {
    return iter.hasNext();
}

public String next() {
    return iter.next();

}

public void reset() {
    iter = keys.iterator();// start again from the beginning
}
}

I would like to iterate through a TreeMap to add objects to a DefaultListModel and then to a JList. However, only the key values are displayed in the list and not the object. How can I correct this?

see code below:

public class ViewInventoryInterface extends JFrame {
private Inventory theInventory; // reference to back end
private InventoryUPCIterator iter;
private DefaultListModel dlm;
private JList list;
private JScrollPane scroll;

public ViewInventoryInterface(Inventory theInventory) {
    this.theInventory = theInventory;
    iter = theInventory.inventoryUPCIterator(); //returns an iterator for the inventory
    dlm = new DefaultListModel();
    while (iter.hasNext()) {
        dlm.addElement(iter.next());
    }
    list = new JList(dlm);
    scroll = new JScrollPane(list);
    setTitle("Inventory");
    add(scroll);
    setSize(400, 400);
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    setLocationRelativeTo(null);
    setVisible(true);

}
}

and this is the class from which the iterator is created

public class InventoryUPCIterator {

private Set<String> keys;
private Iterator<String> iter;

public InventoryUPCIterator(HashMap<String, ItemIndex> inventory) {
    TreeMap<String, ItemIndex> values = new TreeMap<String, ItemIndex>(
            inventory);
    // sorts the index according to the natural ordering of String values
    keys = values.keySet();// returns set of sorted keys
    iter = keys.iterator();
}

public boolean hasNext() {
    return iter.hasNext();
}

public String next() {
    return iter.next();

}

public void reset() {
    iter = keys.iterator();// start again from the beginning
}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

说谎友 2024-12-18 22:27:41

您仅将键值添加到 dlm 对象中,

while (iter.hasNext()) {
        dlm.addElement(iter.next());
    }

修改上面的循环以添加该值

You are only adding the key value into the dlm object

while (iter.hasNext()) {
        dlm.addElement(iter.next());
    }

modify the above loop to add the value as well

∞梦里开花 2024-12-18 22:27:41

不幸的是,您没有展示如何获取迭代器(theInventory.inventoryUPCIterator())。但我相信您写了类似 map.entries().iterator() 的东西,它返回条目的迭代器(即键值对)。使用 map.keySet() 或 key.values() 代替,或者在迭代条目时使用 entry.getKey()

Unfortunately you have not showed how do you get the iterator (theInventory.inventoryUPCIterator()). But I believe that you wrote something like map.entries().iterator() that returns iterator of entries (i.e. key-value pairs). Use map.keySet() or key.values() instead or use entry.getKey() when iterating entries.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文