使用 JList 显示 TreeMap 对象
我想遍历 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您仅将键值添加到 dlm 对象中,
修改上面的循环以添加该值
You are only adding the key value into the dlm object
modify the above loop to add the value as well
不幸的是,您没有展示如何获取迭代器(
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 useentry.getKey()
when iterating entries.