Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6718667
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:59:53+00:00 2026-05-26T08:59:53+00:00

I would like to iterate through a TreeMap to add objects to a DefaultListModel

  • 0

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
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T08:59:54+00:00Added an answer on May 26, 2026 at 8:59 am

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a collection of objects that I would like to iterate through to
I would like to be able to iterate through the values of a struct
I would like to iterate through a CFDictionary (CFPropertyList) and get all values on
I would like to iterate through an xml document to get its values. See
I would like to iterate through an array that covers every pixel on my
Here's the situation: I would like to iterate through a table with input controls,
I would like to randomly iterate through a range. Each value will be visited
Does map() iterate through the list like for would? Is there a value in
I would like to iterate through members of any class in a referenced library
I would like to know how to iterate through all the elements in a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.